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 { return *get(); }
88 // An explicit operator bool so that we can do testing of DI values
90 // FIXME: This operator bool isn't actually protecting anything at the
91 // moment due to the conversion operator above making DIDescriptor nodes
92 // implicitly convertable to bool.
93 explicit operator bool() const { return DbgNode != nullptr; }
95 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
96 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
98 uint16_t getTag() const {
99 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
104 void print(raw_ostream &OS) const;
108 #define DECLARE_SIMPLIFY_DESCRIPTOR(DESC) \
110 template <> struct simplify_type<const DESC>; \
111 template <> struct simplify_type<DESC>;
112 DECLARE_SIMPLIFY_DESCRIPTOR(DIDescriptor)
113 DECLARE_SIMPLIFY_DESCRIPTOR(DISubrange)
114 DECLARE_SIMPLIFY_DESCRIPTOR(DIEnumerator)
115 DECLARE_SIMPLIFY_DESCRIPTOR(DIScope)
116 DECLARE_SIMPLIFY_DESCRIPTOR(DIType)
117 DECLARE_SIMPLIFY_DESCRIPTOR(DIBasicType)
118 DECLARE_SIMPLIFY_DESCRIPTOR(DIDerivedType)
119 DECLARE_SIMPLIFY_DESCRIPTOR(DICompositeType)
120 DECLARE_SIMPLIFY_DESCRIPTOR(DISubroutineType)
121 DECLARE_SIMPLIFY_DESCRIPTOR(DIFile)
122 DECLARE_SIMPLIFY_DESCRIPTOR(DICompileUnit)
123 DECLARE_SIMPLIFY_DESCRIPTOR(DISubprogram)
124 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlock)
125 DECLARE_SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
126 DECLARE_SIMPLIFY_DESCRIPTOR(DINameSpace)
127 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
128 DECLARE_SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
129 DECLARE_SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
130 DECLARE_SIMPLIFY_DESCRIPTOR(DIVariable)
131 DECLARE_SIMPLIFY_DESCRIPTOR(DIExpression)
132 DECLARE_SIMPLIFY_DESCRIPTOR(DILocation)
133 DECLARE_SIMPLIFY_DESCRIPTOR(DIObjCProperty)
134 DECLARE_SIMPLIFY_DESCRIPTOR(DIImportedEntity)
135 #undef DECLARE_SIMPLIFY_DESCRIPTOR
137 typedef DebugNodeArray DIArray;
138 typedef MDTypeRefArray DITypeArray;
140 /// \brief This is used to represent ranges, for array bounds.
141 class DISubrange : public DIDescriptor {
143 DISubrange() = default;
144 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
146 MDSubrange *get() const {
147 return cast_or_null<MDSubrange>(DIDescriptor::get());
149 operator MDSubrange *() const { return get(); }
150 MDSubrange *operator->() const { return get(); }
151 MDSubrange &operator*() const { return *get(); }
153 int64_t getLo() const { return get()->getLowerBound(); }
154 int64_t getCount() const { return get()->getCount(); }
157 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
159 /// FIXME: it seems strange that this doesn't have either a reference to the
160 /// type/precision or a file/line pair for location info.
161 class DIEnumerator : public DIDescriptor {
163 DIEnumerator() = default;
164 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
166 MDEnumerator *get() const {
167 return cast_or_null<MDEnumerator>(DIDescriptor::get());
169 operator MDEnumerator *() const { return get(); }
170 MDEnumerator *operator->() const { return get(); }
171 MDEnumerator &operator*() const { return *get(); }
173 StringRef getName() const { return get()->getName(); }
174 int64_t getEnumValue() const { return get()->getValue(); }
177 template <typename T> class DIRef;
178 typedef DIRef<DIDescriptor> DIDescriptorRef;
179 typedef DIRef<DIScope> DIScopeRef;
180 typedef DIRef<DIType> DITypeRef;
182 /// \brief A base class for various scopes.
184 /// Although, implementation-wise, DIScope is the parent class of most
185 /// other DIxxx classes, including DIType and its descendants, most of
186 /// DIScope's descendants are not a substitutable subtype of
187 /// DIScope. The DIDescriptor::isScope() method only is true for
188 /// DIScopes that are scopes in the strict lexical scope sense
189 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
190 class DIScope : public DIDescriptor {
193 DIScope(const MDScope *N) : DIDescriptor(N) {}
195 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
196 operator MDScope *() const { return get(); }
197 MDScope *operator->() const { return get(); }
198 MDScope &operator*() const { return *get(); }
200 inline DIScopeRef getContext() const;
201 StringRef getName() const { return get()->getName(); }
202 StringRef getFilename() const { return get()->getFilename(); }
203 StringRef getDirectory() const { return get()->getDirectory(); }
205 /// \brief Generate a reference to this DIScope.
207 /// Uses the type identifier instead of the actual MDNode if possible, to
208 /// help type uniquing.
209 DIScopeRef getRef() const;
212 /// \brief Represents reference to a DIDescriptor.
214 /// Abstracts over direct and identifier-based metadata references.
215 template <typename T> class DIRef {
216 /// \brief Val can be either a MDNode or a MDString.
218 /// In the latter, MDString specifies the type identifier.
223 DIRef(const TypedDebugNodeRef<U> &Ref,
224 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
228 T resolve(const DITypeIdentifierMap &Map) const;
229 operator Metadata *() const { return const_cast<Metadata *>(Val); }
233 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
235 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
236 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
238 DIScopeRef DIScope::getContext() const { return get()->getScope(); }
240 /// \brief This is a wrapper for a type.
242 /// FIXME: Types should be factored much better so that CV qualifiers and
243 /// others do not require a huge and empty descriptor full of zeros.
244 class DIType : public DIScope {
247 DIType(const MDType *N) : DIScope(N) {}
249 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
250 operator MDType *() const { return get(); }
251 MDType *operator->() const { return get(); }
252 MDType &operator*() const { return *get(); }
254 DIScopeRef getContext() const { return get()->getScope(); }
255 StringRef getName() const { return get()->getName(); }
256 unsigned getLineNumber() const { return get()->getLine(); }
257 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
258 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
259 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
260 // carry this is just plain insane.
261 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
262 unsigned getFlags() const { return get()->getFlags(); }
264 bool isPrivate() const { return get()->isPrivate(); }
265 bool isProtected() const { return get()->isProtected(); }
266 bool isPublic() const { return get()->isPublic(); }
267 bool isForwardDecl() const { return get()->isForwardDecl(); }
268 bool isAppleBlockExtension() const { return get()->isAppleBlockExtension(); }
269 bool isBlockByrefStruct() const { return get()->isBlockByrefStruct(); }
270 bool isVirtual() const { return get()->isVirtual(); }
271 bool isArtificial() const { return get()->isArtificial(); }
272 bool isObjectPointer() const { return get()->isObjectPointer(); }
273 bool isObjcClassComplete() const { return get()->isObjcClassComplete(); }
274 bool isVector() const { return get()->isVector(); }
275 bool isStaticMember() const { return get()->isStaticMember(); }
276 bool isLValueReference() const { return get()->isLValueReference(); }
277 bool isRValueReference() const { return get()->isRValueReference(); }
279 bool isValid() const { return DbgNode && isa<MDType>(*this); }
282 /// \brief A basic type, like 'int' or 'float'.
283 class DIBasicType : public DIType {
285 DIBasicType() = default;
286 DIBasicType(const MDBasicType *N) : DIType(N) {}
288 MDBasicType *get() const {
289 return cast_or_null<MDBasicType>(DIDescriptor::get());
291 operator MDBasicType *() const { return get(); }
292 MDBasicType *operator->() const { return get(); }
293 MDBasicType &operator*() const { return *get(); }
295 unsigned getEncoding() const { return get()->getEncoding(); }
298 /// \brief A simple derived type
300 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
301 /// Or, a data member of a class/struct/union.
302 class DIDerivedType : public DIType {
304 DIDerivedType() = default;
305 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
307 MDDerivedTypeBase *get() const {
308 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
310 operator MDDerivedTypeBase *() const { return get(); }
311 MDDerivedTypeBase *operator->() const { return get(); }
312 MDDerivedTypeBase &operator*() const { return *get(); }
314 DITypeRef getTypeDerivedFrom() const { return get()->getBaseType(); }
316 /// \brief Return property node, if this ivar is associated with one.
317 MDNode *getObjCProperty() const {
318 if (auto *N = dyn_cast<MDDerivedType>(get()))
319 return dyn_cast_or_null<MDNode>(N->getExtraData());
323 DITypeRef getClassType() const {
324 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
325 if (auto *N = dyn_cast<MDDerivedType>(get()))
326 return MDTypeRef(N->getExtraData());
330 Constant *getConstant() const {
331 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
332 if (auto *N = dyn_cast<MDDerivedType>(get()))
333 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
334 return C->getValue();
340 /// \brief Types that refer to multiple other types.
342 /// This descriptor holds a type that can refer to multiple other types, like a
343 /// function or struct.
345 /// DICompositeType is derived from DIDerivedType because some
346 /// composite types (such as enums) can be derived from basic types
347 // FIXME: Make this derive from DIType directly & just store the
348 // base type in a single DIType field.
349 class DICompositeType : public DIDerivedType {
350 friend class DIBuilder;
353 DICompositeType() = default;
354 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
356 MDCompositeTypeBase *get() const {
357 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
359 operator MDCompositeTypeBase *() const { return get(); }
360 MDCompositeTypeBase *operator->() const { return get(); }
361 MDCompositeTypeBase &operator*() const { return *get(); }
363 DIArray getElements() const {
364 assert(!isa<MDSubroutineType>(*this) && "no elements for DISubroutineType");
365 return DIArray(get()->getElements());
368 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
369 DITypeRef getContainingType() const { return get()->getVTableHolder(); }
371 DIArray getTemplateParams() const { return get()->getTemplateParams(); }
372 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
375 class DISubroutineType : public DICompositeType {
377 DISubroutineType() = default;
378 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
380 MDSubroutineType *get() const {
381 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
383 operator MDSubroutineType *() const { return get(); }
384 MDSubroutineType *operator->() const { return get(); }
385 MDSubroutineType &operator*() const { return *get(); }
387 MDTypeRefArray getTypeArray() const { return get()->getTypeArray(); }
390 /// \brief This is a wrapper for a file.
391 class DIFile : public DIScope {
394 DIFile(const MDFile *N) : DIScope(N) {}
396 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
397 operator MDFile *() const { return get(); }
398 MDFile *operator->() const { return get(); }
399 MDFile &operator*() const { return *get(); }
401 /// \brief Retrieve the MDNode for the directory/file pair.
402 MDNode *getFileNode() const { return get(); }
405 /// \brief A wrapper for a compile unit.
406 class DICompileUnit : public DIScope {
408 DICompileUnit() = default;
409 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
411 MDCompileUnit *get() const {
412 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
414 operator MDCompileUnit *() const { return get(); }
415 MDCompileUnit *operator->() const { return get(); }
416 MDCompileUnit &operator*() const { return *get(); }
418 dwarf::SourceLanguage getLanguage() const {
419 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
421 StringRef getProducer() const { return get()->getProducer(); }
422 bool isOptimized() const { return get()->isOptimized(); }
423 StringRef getFlags() const { return get()->getFlags(); }
424 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
426 DIArray getEnumTypes() const { return get()->getEnumTypes(); }
427 DIArray getRetainedTypes() const { return get()->getRetainedTypes(); }
428 DIArray getSubprograms() const { return get()->getSubprograms(); }
429 DIArray getGlobalVariables() const { return get()->getGlobalVariables(); }
430 DIArray getImportedEntities() const { return get()->getImportedEntities(); }
432 void replaceSubprograms(DIArray Subprograms);
433 void replaceGlobalVariables(DIArray GlobalVariables);
435 StringRef getSplitDebugFilename() const {
436 return get()->getSplitDebugFilename();
438 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
441 /// \brief This is a wrapper for a subprogram (e.g. a function).
442 class DISubprogram : public DIScope {
444 DISubprogram() = default;
445 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
447 MDSubprogram *get() const {
448 return cast_or_null<MDSubprogram>(DIDescriptor::get());
450 operator MDSubprogram *() const { return get(); }
451 MDSubprogram *operator->() const { return get(); }
452 MDSubprogram &operator*() const { return *get(); }
454 StringRef getName() const { return get()->getName(); }
455 StringRef getDisplayName() const { return get()->getDisplayName(); }
456 StringRef getLinkageName() const { return get()->getLinkageName(); }
457 unsigned getLineNumber() const { return get()->getLine(); }
459 /// \brief Check if this is local (like 'static' in C).
460 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
461 unsigned isDefinition() const { return get()->isDefinition(); }
463 unsigned getVirtuality() const { return get()->getVirtuality(); }
464 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
466 unsigned getFlags() const { return get()->getFlags(); }
468 unsigned isOptimized() const { return get()->isOptimized(); }
470 /// \brief Get the beginning of the scope of the function (not the name).
471 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
473 DIScopeRef getContext() const { return get()->getScope(); }
474 DISubroutineType getType() const { return get()->getType(); }
476 DITypeRef getContainingType() const { return get()->getContainingType(); }
478 /// \brief Check if this provides debugging information for the function F.
479 bool describes(const Function *F) const { return get()->describes(F); }
481 Function *getFunction() const { return get()->getFunction(); }
483 void replaceFunction(Function *F) { get()->replaceFunction(F); }
484 DIArray getTemplateParams() const { return get()->getTemplateParams(); }
485 DISubprogram getFunctionDeclaration() const {
486 return get()->getDeclaration();
488 DIArray getVariables() const { return DIArray(get()->getVariables()); }
490 unsigned isArtificial() const { return get()->isArtificial(); }
491 bool isPrivate() const { return get()->isPrivate(); }
492 bool isProtected() const { return get()->isProtected(); }
493 bool isPublic() const { return get()->isPublic(); }
494 bool isExplicit() const { return get()->isExplicit(); }
495 bool isPrototyped() const { return get()->isPrototyped(); }
496 unsigned isLValueReference() const { return get()->isLValueReference(); }
497 unsigned isRValueReference() const { return get()->isRValueReference(); }
500 /// \brief This is a wrapper for a lexical block.
501 class DILexicalBlock : public DIScope {
503 DILexicalBlock() = default;
504 DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
506 MDLexicalBlockBase *get() const {
507 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
509 operator MDLexicalBlockBase *() const { return get(); }
510 MDLexicalBlockBase *operator->() const { return get(); }
511 MDLexicalBlockBase &operator*() const { return *get(); }
513 DIScope getContext() const { return DIScope(get()->getScope()); }
514 unsigned getLineNumber() const {
515 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
519 unsigned getColumnNumber() const {
520 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
521 return N->getColumn();
526 /// \brief This is a wrapper for a lexical block with a filename change.
527 class DILexicalBlockFile : public DIScope {
529 DILexicalBlockFile() = default;
530 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
532 MDLexicalBlockFile *get() const {
533 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
535 operator MDLexicalBlockFile *() const { return get(); }
536 MDLexicalBlockFile *operator->() const { return get(); }
537 MDLexicalBlockFile &operator*() const { return *get(); }
539 DIScope getContext() const { return get()->getScope(); }
540 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
543 /// \brief A wrapper for a C++ style name space.
544 class DINameSpace : public DIScope {
546 DINameSpace() = default;
547 DINameSpace(const MDNamespace *N) : DIScope(N) {}
549 MDNamespace *get() const {
550 return cast_or_null<MDNamespace>(DIDescriptor::get());
552 operator MDNamespace *() const { return get(); }
553 MDNamespace *operator->() const { return get(); }
554 MDNamespace &operator*() const { return *get(); }
556 StringRef getName() const { return get()->getName(); }
557 unsigned getLineNumber() const { return get()->getLine(); }
558 DIScope getContext() const { return DIScope(get()->getScope()); }
561 /// \brief This is a wrapper for template type parameter.
562 class DITemplateTypeParameter : public DIDescriptor {
564 DITemplateTypeParameter() = default;
565 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
567 MDTemplateTypeParameter *get() const {
568 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
570 operator MDTemplateTypeParameter *() const { return get(); }
571 MDTemplateTypeParameter *operator->() const { return get(); }
572 MDTemplateTypeParameter &operator*() const { return *get(); }
574 StringRef getName() const { return get()->getName(); }
575 DITypeRef getType() const { return get()->getType(); }
578 /// \brief This is a wrapper for template value parameter.
579 class DITemplateValueParameter : public DIDescriptor {
581 DITemplateValueParameter() = default;
582 DITemplateValueParameter(const MDTemplateValueParameter *N)
585 MDTemplateValueParameter *get() const {
586 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
588 operator MDTemplateValueParameter *() const { return get(); }
589 MDTemplateValueParameter *operator->() const { return get(); }
590 MDTemplateValueParameter &operator*() const { return *get(); }
592 StringRef getName() const { return get()->getName(); }
593 DITypeRef getType() const { return get()->getType(); }
594 Metadata *getValue() const { return get()->getValue(); }
597 /// \brief This is a wrapper for a global variable.
598 class DIGlobalVariable : public DIDescriptor {
599 DIFile getFile() const { return DIFile(get()->getFile()); }
602 DIGlobalVariable() = default;
603 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
605 MDGlobalVariable *get() const {
606 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
608 operator MDGlobalVariable *() const { return get(); }
609 MDGlobalVariable *operator->() const { return get(); }
610 MDGlobalVariable &operator*() const { return *get(); }
612 StringRef getName() const { return get()->getName(); }
613 StringRef getDisplayName() const { return get()->getDisplayName(); }
614 StringRef getLinkageName() const { return get()->getLinkageName(); }
615 unsigned getLineNumber() const { return get()->getLine(); }
616 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
617 unsigned isDefinition() const { return get()->isDefinition(); }
619 DIScope getContext() const { return get()->getScope(); }
620 StringRef getFilename() const { return get()->getFilename(); }
621 StringRef getDirectory() const { return get()->getDirectory(); }
622 DITypeRef getType() const { return get()->getType(); }
624 Constant *getConstant() const { return get()->getVariable(); }
625 DIDerivedType getStaticDataMemberDeclaration() const {
626 return get()->getStaticDataMemberDeclaration();
630 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
631 class DIVariable : public DIDescriptor {
632 unsigned getFlags() const { return get()->getFlags(); }
635 DIVariable() = default;
636 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
638 MDLocalVariable *get() const {
639 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
641 operator MDLocalVariable *() const { return get(); }
642 MDLocalVariable *operator->() const { return get(); }
643 MDLocalVariable &operator*() const { return *get(); }
645 StringRef getName() const { return get()->getName(); }
646 unsigned getLineNumber() const { return get()->getLine(); }
647 unsigned getArgNumber() const { return get()->getArg(); }
649 DIScope getContext() const { return get()->getScope(); }
650 DIFile getFile() const { return get()->getFile(); }
651 DITypeRef getType() const { return get()->getType(); }
653 bool isArtificial() const { return get()->isArtificial(); }
654 bool isObjectPointer() const { return get()->isObjectPointer(); }
656 /// \brief If this variable is inlined then return inline location.
657 MDNode *getInlinedAt() const { return get()->getInlinedAt(); }
659 /// \brief Check if this is a "__block" variable (Apple Blocks).
660 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
661 return (getType().resolve(Map)).isBlockByrefStruct();
664 void printExtendedName(raw_ostream &OS) const;
667 /// \brief A complex location expression in postfix notation.
669 /// This is (almost) a DWARF expression that modifies the location of a
670 /// variable or (or the location of a single piece of a variable).
672 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
673 /// and have DW_OP_plus consume the topmost elements on the stack.
674 class DIExpression : public DIDescriptor {
676 DIExpression() = default;
677 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
679 MDExpression *get() const {
680 return cast_or_null<MDExpression>(DIDescriptor::get());
682 operator MDExpression *() const { return get(); }
683 MDExpression *operator->() const { return get(); }
684 MDExpression &operator*() const { return *get(); }
686 unsigned getNumElements() const { return get()->getNumElements(); }
687 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
688 bool isBitPiece() const { return get()->isBitPiece(); }
689 uint64_t getBitPieceOffset() const { return get()->getBitPieceOffset(); }
690 uint64_t getBitPieceSize() const { return get()->getBitPieceSize(); }
693 /// \brief This object holds location information.
695 /// This object is not associated with any DWARF tag.
696 class DILocation : public DIDescriptor {
698 DILocation() = default;
699 DILocation(const MDLocation *N) : DIDescriptor(N) {}
701 MDLocation *get() const {
702 return cast_or_null<MDLocation>(DIDescriptor::get());
704 operator MDLocation *() const { return get(); }
705 MDLocation *operator->() const { return get(); }
706 MDLocation &operator*() const { return *get(); }
708 unsigned getLineNumber() const { return get()->getLine(); }
709 unsigned getColumnNumber() const { return get()->getColumn(); }
710 DIScope getScope() const { return DIScope(get()->getScope()); }
711 DILocation getOrigLocation() const { return get()->getInlinedAt(); }
712 StringRef getFilename() const { return get()->getFilename(); }
713 StringRef getDirectory() const { return get()->getDirectory(); }
715 /// \brief Get the DWAF discriminator.
717 /// DWARF discriminators are used to distinguish identical file locations for
718 /// instructions that are on different basic blocks. If two instructions are
719 /// inside the same lexical block and are in different basic blocks, we
720 /// create a new lexical block with identical location as the original but
721 /// with a different discriminator value
722 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
723 unsigned getDiscriminator() const {
724 // Since discriminators are associated with lexical blocks, make
725 // sure this location is a lexical block before retrieving its
727 if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
728 return F->getDiscriminator();
732 /// \brief Generate a new discriminator value for this location.
733 unsigned computeNewDiscriminator(LLVMContext &Ctx);
735 /// \brief Return a copy of this location with a different scope.
736 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
739 class DIObjCProperty : public DIDescriptor {
741 DIObjCProperty() = default;
742 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
744 MDObjCProperty *get() const {
745 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
747 operator MDObjCProperty *() const { return get(); }
748 MDObjCProperty *operator->() const { return get(); }
749 MDObjCProperty &operator*() const { return *get(); }
751 StringRef getObjCPropertyName() const { return get()->getName(); }
752 DIFile getFile() const { return get()->getFile(); }
753 unsigned getLineNumber() const { return get()->getLine(); }
755 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
756 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
757 unsigned getAttributes() const { return get()->getAttributes(); }
758 bool isReadOnlyObjCProperty() const {
759 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
761 bool isReadWriteObjCProperty() const {
762 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
764 bool isAssignObjCProperty() const {
765 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
767 bool isRetainObjCProperty() const {
768 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
770 bool isCopyObjCProperty() const {
771 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
773 bool isNonAtomicObjCProperty() const {
774 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
777 /// \brief Get the type.
779 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
780 /// the type as a DITypeRef here.
781 DIType getType() const { return get()->getType(); }
784 /// \brief An imported module (C++ using directive or similar).
785 class DIImportedEntity : public DIDescriptor {
787 DIImportedEntity() = default;
788 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
790 MDImportedEntity *get() const {
791 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
793 operator MDImportedEntity *() const { return get(); }
794 MDImportedEntity *operator->() const { return get(); }
795 MDImportedEntity &operator*() const { return *get(); }
797 DIScope getContext() const { return get()->getScope(); }
798 DIDescriptorRef getEntity() const { return get()->getEntity(); }
799 unsigned getLineNumber() const { return get()->getLine(); }
800 StringRef getName() const { return get()->getName(); }
803 #define SIMPLIFY_DESCRIPTOR(DESC) \
804 template <> struct simplify_type<const DESC> { \
805 typedef Metadata *SimpleType; \
806 static SimpleType getSimplifiedValue(const DESC &DI) { return DI; } \
808 template <> struct simplify_type<DESC> : simplify_type<const DESC> {};
809 SIMPLIFY_DESCRIPTOR(DIDescriptor)
810 SIMPLIFY_DESCRIPTOR(DISubrange)
811 SIMPLIFY_DESCRIPTOR(DIEnumerator)
812 SIMPLIFY_DESCRIPTOR(DIScope)
813 SIMPLIFY_DESCRIPTOR(DIType)
814 SIMPLIFY_DESCRIPTOR(DIBasicType)
815 SIMPLIFY_DESCRIPTOR(DIDerivedType)
816 SIMPLIFY_DESCRIPTOR(DICompositeType)
817 SIMPLIFY_DESCRIPTOR(DISubroutineType)
818 SIMPLIFY_DESCRIPTOR(DIFile)
819 SIMPLIFY_DESCRIPTOR(DICompileUnit)
820 SIMPLIFY_DESCRIPTOR(DISubprogram)
821 SIMPLIFY_DESCRIPTOR(DILexicalBlock)
822 SIMPLIFY_DESCRIPTOR(DILexicalBlockFile)
823 SIMPLIFY_DESCRIPTOR(DINameSpace)
824 SIMPLIFY_DESCRIPTOR(DITemplateTypeParameter)
825 SIMPLIFY_DESCRIPTOR(DITemplateValueParameter)
826 SIMPLIFY_DESCRIPTOR(DIGlobalVariable)
827 SIMPLIFY_DESCRIPTOR(DIVariable)
828 SIMPLIFY_DESCRIPTOR(DIExpression)
829 SIMPLIFY_DESCRIPTOR(DILocation)
830 SIMPLIFY_DESCRIPTOR(DIObjCProperty)
831 SIMPLIFY_DESCRIPTOR(DIImportedEntity)
832 #undef SIMPLIFY_DESCRIPTOR
834 /// \brief Find subprogram that is enclosing this scope.
835 DISubprogram getDISubprogram(const MDNode *Scope);
837 /// \brief Find debug info for a given function.
838 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
840 DISubprogram getDISubprogram(const Function *F);
842 /// \brief Find underlying composite type.
843 DICompositeType getDICompositeType(DIType T);
845 /// \brief Create a new inlined variable based on current variable.
847 /// @param DV Current Variable.
848 /// @param InlinedScope Location at current variable is inlined.
849 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
850 LLVMContext &VMContext);
852 /// \brief Remove inlined scope from the variable.
853 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
855 /// \brief Generate map by visiting all retained types.
856 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
858 /// \brief Strip debug info in the module if it exists.
860 /// To do this, we remove all calls to the debugger intrinsics and any named
861 /// metadata for debugging. We also remove debug locations for instructions.
862 /// Return true if module is modified.
863 bool StripDebugInfo(Module &M);
864 bool stripDebugInfo(Function &F);
866 /// \brief Return Debug Info Metadata Version by checking module flags.
867 unsigned getDebugMetadataVersionFromModule(const Module &M);
869 /// \brief Utility to find all debug info in a module.
871 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
872 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
873 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
874 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
875 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
877 class DebugInfoFinder {
879 DebugInfoFinder() : TypeMapInitialized(false) {}
881 /// \brief Process entire module and collect debug info anchors.
882 void processModule(const Module &M);
884 /// \brief Process DbgDeclareInst.
885 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
886 /// \brief Process DbgValueInst.
887 void processValue(const Module &M, const DbgValueInst *DVI);
888 /// \brief Process DILocation.
889 void processLocation(const Module &M, DILocation Loc);
891 /// \brief Clear all lists.
895 void InitializeTypeMap(const Module &M);
897 void processType(DIType DT);
898 void processSubprogram(DISubprogram SP);
899 void processScope(DIScope Scope);
900 bool addCompileUnit(DICompileUnit CU);
901 bool addGlobalVariable(DIGlobalVariable DIG);
902 bool addSubprogram(DISubprogram SP);
903 bool addType(DIType DT);
904 bool addScope(DIScope Scope);
907 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
908 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
909 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
910 global_variable_iterator;
911 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
912 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
914 iterator_range<compile_unit_iterator> compile_units() const {
915 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
918 iterator_range<subprogram_iterator> subprograms() const {
919 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
922 iterator_range<global_variable_iterator> global_variables() const {
923 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
926 iterator_range<type_iterator> types() const {
927 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
930 iterator_range<scope_iterator> scopes() const {
931 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
934 unsigned compile_unit_count() const { return CUs.size(); }
935 unsigned global_variable_count() const { return GVs.size(); }
936 unsigned subprogram_count() const { return SPs.size(); }
937 unsigned type_count() const { return TYs.size(); }
938 unsigned scope_count() const { return Scopes.size(); }
941 SmallVector<DICompileUnit, 8> CUs;
942 SmallVector<DISubprogram, 8> SPs;
943 SmallVector<DIGlobalVariable, 8> GVs;
944 SmallVector<DIType, 8> TYs;
945 SmallVector<DIScope, 8> Scopes;
946 SmallPtrSet<MDNode *, 64> NodesSeen;
947 DITypeIdentifierMap TypeIdentifierMap;
949 /// \brief Specify if TypeIdentifierMap is initialized.
950 bool TypeMapInitialized;
953 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
955 } // end namespace llvm