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
80 static unsigned getFlag(StringRef Flag);
81 static const char *getFlagString(unsigned Flag);
83 /// \brief Split up a flags bitfield.
85 /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
86 /// any remaining (unrecognized) bits.
87 static unsigned splitFlags(unsigned Flags,
88 SmallVectorImpl<unsigned> &SplitFlags);
91 const MDNode *DbgNode;
93 DIDescriptor getDescriptorField(unsigned Elt) const;
94 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
95 return DescTy(getDescriptorField(Elt));
99 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
103 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
104 operator MDNode *() const { return get(); }
105 MDNode *operator->() const { return get(); }
106 MDNode &operator*() const {
107 assert(get() && "Expected valid pointer");
111 // An explicit operator bool so that we can do testing of DI values
113 // FIXME: This operator bool isn't actually protecting anything at the
114 // moment due to the conversion operator above making DIDescriptor nodes
115 // implicitly convertable to bool.
116 explicit operator bool() const { return DbgNode != nullptr; }
118 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
119 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
121 uint16_t getTag() const {
122 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
127 bool isDerivedType() const { return get() && isa<MDDerivedTypeBase>(get()); }
128 bool isCompositeType() const {
129 return get() && isa<MDCompositeTypeBase>(get());
131 bool isSubroutineType() const {
132 return get() && isa<MDSubroutineType>(get());
134 bool isBasicType() const { return get() && isa<MDBasicType>(get()); }
135 bool isVariable() const { return get() && isa<MDLocalVariable>(get()); }
136 bool isSubprogram() const { return get() && isa<MDSubprogram>(get()); }
137 bool isGlobalVariable() const {
138 return get() && isa<MDGlobalVariable>(get());
140 bool isScope() const { return get() && isa<MDScope>(get()); }
141 bool isFile() const { return get() && isa<MDFile>(get()); }
142 bool isCompileUnit() const { return get() && isa<MDCompileUnit>(get()); }
143 bool isNameSpace() const{ return get() && isa<MDNamespace>(get()); }
144 bool isLexicalBlockFile() const {
145 return get() && isa<MDLexicalBlockFile>(get());
147 bool isLexicalBlock() const {
148 return get() && isa<MDLexicalBlockBase>(get());
150 bool isSubrange() const { return get() && isa<MDSubrange>(get()); }
151 bool isEnumerator() const { return get() && isa<MDEnumerator>(get()); }
152 bool isType() const { return get() && isa<MDType>(get()); }
153 bool isTemplateTypeParameter() const {
154 return get() && isa<MDTemplateTypeParameter>(get());
156 bool isTemplateValueParameter() const {
157 return get() && isa<MDTemplateValueParameter>(get());
159 bool isObjCProperty() const { return get() && isa<MDObjCProperty>(get()); }
160 bool isImportedEntity() const {
161 return get() && isa<MDImportedEntity>(get());
163 bool isExpression() const { return get() && isa<MDExpression>(get()); }
165 void print(raw_ostream &OS) const;
168 /// \brief Replace all uses of debug info referenced by this descriptor.
169 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
170 void replaceAllUsesWith(MDNode *D);
173 /// \brief This is used to represent ranges, for array bounds.
174 class DISubrange : public DIDescriptor {
176 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
177 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
179 MDSubrange *get() const {
180 return cast_or_null<MDSubrange>(DIDescriptor::get());
182 operator MDSubrange *() const { return get(); }
183 MDSubrange *operator->() const { return get(); }
184 MDSubrange &operator*() const {
185 assert(get() && "Expected valid pointer");
189 int64_t getLo() const { return get()->getLo(); }
190 int64_t getCount() const { return get()->getCount(); }
194 /// \brief This descriptor holds an array of nodes with type T.
195 template <typename T> class DITypedArray : public DIDescriptor {
197 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
198 operator MDTuple *() const {
199 return const_cast<MDTuple *>(cast_or_null<MDTuple>(DbgNode));
201 unsigned getNumElements() const {
202 return DbgNode ? DbgNode->getNumOperands() : 0;
204 T getElement(unsigned Idx) const { return getFieldAs<T>(Idx); }
207 typedef DITypedArray<DIDescriptor> DIArray;
209 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
211 /// FIXME: it seems strange that this doesn't have either a reference to the
212 /// type/precision or a file/line pair for location info.
213 class DIEnumerator : public DIDescriptor {
215 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
216 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
218 MDEnumerator *get() const {
219 return cast_or_null<MDEnumerator>(DIDescriptor::get());
221 operator MDEnumerator *() const { return get(); }
222 MDEnumerator *operator->() const { return get(); }
223 MDEnumerator &operator*() const {
224 assert(get() && "Expected valid pointer");
228 StringRef getName() const { return get()->getName(); }
229 int64_t getEnumValue() const { return get()->getValue(); }
233 template <typename T> class DIRef;
234 typedef DIRef<DIDescriptor> DIDescriptorRef;
235 typedef DIRef<DIScope> DIScopeRef;
236 typedef DIRef<DIType> DITypeRef;
237 typedef DITypedArray<DITypeRef> DITypeArray;
239 /// \brief A base class for various scopes.
241 /// Although, implementation-wise, DIScope is the parent class of most
242 /// other DIxxx classes, including DIType and its descendants, most of
243 /// DIScope's descendants are not a substitutable subtype of
244 /// DIScope. The DIDescriptor::isScope() method only is true for
245 /// DIScopes that are scopes in the strict lexical scope sense
246 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
247 class DIScope : public DIDescriptor {
249 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
250 DIScope(const MDScope *N) : DIDescriptor(N) {}
252 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
253 operator MDScope *() const { return get(); }
254 MDScope *operator->() const { return get(); }
255 MDScope &operator*() const {
256 assert(get() && "Expected valid pointer");
260 /// \brief Get the parent scope.
262 /// Gets the parent scope for this scope node or returns a default
263 /// constructed scope.
264 DIScopeRef getContext() const;
265 /// \brief Get the scope name.
267 /// If the scope node has a name, return that, else return an empty string.
268 StringRef getName() const;
269 StringRef getFilename() const;
270 StringRef getDirectory() const;
272 /// \brief Generate a reference to this DIScope.
274 /// Uses the type identifier instead of the actual MDNode if possible, to
275 /// help type uniquing.
276 DIScopeRef getRef() const;
279 /// \brief Represents reference to a DIDescriptor.
281 /// Abstracts over direct and identifier-based metadata references.
282 template <typename T> class DIRef {
283 template <typename DescTy>
284 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
285 friend DIScopeRef DIScope::getContext() const;
286 friend DIScopeRef DIScope::getRef() const;
289 /// \brief Val can be either a MDNode or a MDString.
291 /// In the latter, MDString specifies the type identifier.
293 explicit DIRef(const Metadata *V);
297 DIRef(const TypedDebugNodeRef<U> &Ref,
298 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
302 T resolve(const DITypeIdentifierMap &Map) const;
303 operator Metadata *() const { return const_cast<Metadata *>(Val); }
305 static DIRef get(const Metadata *MD) { return DIRef(MD); }
308 template <typename T>
309 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
313 if (const MDNode *MD = dyn_cast<MDNode>(Val))
316 const MDString *MS = cast<MDString>(Val);
317 // Find the corresponding MDNode.
318 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
319 assert(Iter != Map.end() && "Identifier not in the type map?");
320 assert(DIDescriptor(Iter->second).isType() &&
321 "MDNode in DITypeIdentifierMap should be a DIType.");
322 return T(Iter->second);
325 /// \brief Handle fields that are references to DIDescriptors.
327 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
328 /// \brief Specialize DIRef constructor for DIDescriptorRef.
329 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
331 /// \brief Handle fields that are references to DIScopes.
332 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
333 /// \brief Specialize DIRef constructor for DIScopeRef.
334 template <> DIRef<DIScope>::DIRef(const Metadata *V);
336 /// \brief Handle fields that are references to DITypes.
337 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
338 /// \brief Specialize DIRef constructor for DITypeRef.
339 template <> DIRef<DIType>::DIRef(const Metadata *V);
341 /// \brief This is a wrapper for a type.
343 /// FIXME: Types should be factored much better so that CV qualifiers and
344 /// others do not require a huge and empty descriptor full of zeros.
345 class DIType : public DIScope {
347 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
348 DIType(const MDType *N) : DIScope(N) {}
350 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
351 operator MDType *() const { return get(); }
352 MDType *operator->() const { return get(); }
353 MDType &operator*() const {
354 assert(get() && "Expected valid pointer");
360 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
361 StringRef getName() const { return get()->getName(); }
362 unsigned getLineNumber() const { return get()->getLine(); }
363 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
364 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
365 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
366 // carry this is just plain insane.
367 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
368 unsigned getFlags() const { return get()->getFlags(); }
369 bool isPrivate() const {
370 return (getFlags() & FlagAccessibility) == FlagPrivate;
372 bool isProtected() const {
373 return (getFlags() & FlagAccessibility) == FlagProtected;
375 bool isPublic() const {
376 return (getFlags() & FlagAccessibility) == FlagPublic;
378 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
379 bool isAppleBlockExtension() const {
380 return (getFlags() & FlagAppleBlock) != 0;
382 bool isBlockByrefStruct() const {
383 return (getFlags() & FlagBlockByrefStruct) != 0;
385 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
386 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
387 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
388 bool isObjcClassComplete() const {
389 return (getFlags() & FlagObjcClassComplete) != 0;
391 bool isVector() const { return (getFlags() & FlagVector) != 0; }
392 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
393 bool isLValueReference() const {
394 return (getFlags() & FlagLValueReference) != 0;
396 bool isRValueReference() const {
397 return (getFlags() & FlagRValueReference) != 0;
399 bool isValid() const { return DbgNode && isType(); }
402 /// \brief A basic type, like 'int' or 'float'.
403 class DIBasicType : public DIType {
405 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
406 DIBasicType(const MDBasicType *N) : DIType(N) {}
408 MDBasicType *get() const {
409 return cast_or_null<MDBasicType>(DIDescriptor::get());
411 operator MDBasicType *() const { return get(); }
412 MDBasicType *operator->() const { return get(); }
413 MDBasicType &operator*() const {
414 assert(get() && "Expected valid pointer");
418 unsigned getEncoding() const { return get()->getEncoding(); }
423 /// \brief A simple derived type
425 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
426 /// Or, a data member of a class/struct/union.
427 class DIDerivedType : public DIType {
429 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
430 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
432 MDDerivedTypeBase *get() const {
433 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
435 operator MDDerivedTypeBase *() const { return get(); }
436 MDDerivedTypeBase *operator->() const { return get(); }
437 MDDerivedTypeBase &operator*() const {
438 assert(get() && "Expected valid pointer");
442 DITypeRef getTypeDerivedFrom() const {
443 return DITypeRef::get(get()->getBaseType());
446 /// \brief Return property node, if this ivar is associated with one.
447 MDNode *getObjCProperty() const {
448 if (auto *N = dyn_cast<MDDerivedType>(get()))
449 return dyn_cast_or_null<MDNode>(N->getExtraData());
453 DITypeRef getClassType() const {
454 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
455 if (auto *N = dyn_cast<MDDerivedType>(get()))
456 return DITypeRef::get(N->getExtraData());
457 return DITypeRef::get(nullptr);
460 Constant *getConstant() const {
461 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
462 if (auto *N = dyn_cast<MDDerivedType>(get()))
463 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
464 return C->getValue();
472 /// \brief Types that refer to multiple other types.
474 /// This descriptor holds a type that can refer to multiple other types, like a
475 /// function or struct.
477 /// DICompositeType is derived from DIDerivedType because some
478 /// composite types (such as enums) can be derived from basic types
479 // FIXME: Make this derive from DIType directly & just store the
480 // base type in a single DIType field.
481 class DICompositeType : public DIDerivedType {
482 friend class DIBuilder;
484 /// \brief Set the array of member DITypes.
485 void setArraysHelper(MDNode *Elements, MDNode *TParams);
488 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
489 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
491 MDCompositeTypeBase *get() const {
492 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
494 operator MDCompositeTypeBase *() const { return get(); }
495 MDCompositeTypeBase *operator->() const { return get(); }
496 MDCompositeTypeBase &operator*() const {
497 assert(get() && "Expected valid pointer");
501 DIArray getElements() const {
502 assert(!isSubroutineType() && "no elements for DISubroutineType");
503 return DIArray(get()->getElements());
507 template <typename T>
508 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
510 (!TParams || DbgNode->getNumOperands() == 8) &&
511 "If you're setting the template parameters this should include a slot "
513 setArraysHelper(Elements, TParams);
517 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
518 DITypeRef getContainingType() const {
519 return DITypeRef::get(get()->getVTableHolder());
523 /// \brief Set the containing type.
524 void setContainingType(DICompositeType ContainingType);
527 DIArray getTemplateParams() const {
528 return DIArray(get()->getTemplateParams());
530 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
535 class DISubroutineType : public DICompositeType {
537 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
538 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
540 MDSubroutineType *get() const {
541 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
543 operator MDSubroutineType *() const { return get(); }
544 MDSubroutineType *operator->() const { return get(); }
545 MDSubroutineType &operator*() const {
546 assert(get() && "Expected valid pointer");
550 DITypedArray<DITypeRef> getTypeArray() const {
551 return DITypedArray<DITypeRef>(get()->getTypeArray());
555 /// \brief This is a wrapper for a file.
556 class DIFile : public DIScope {
558 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
559 DIFile(const MDFile *N) : DIScope(N) {}
561 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
562 operator MDFile *() const { return get(); }
563 MDFile *operator->() const { return get(); }
564 MDFile &operator*() const {
565 assert(get() && "Expected valid pointer");
569 /// \brief Retrieve the MDNode for the directory/file pair.
570 MDNode *getFileNode() const { return get(); }
574 /// \brief A wrapper for a compile unit.
575 class DICompileUnit : public DIScope {
577 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
578 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
580 MDCompileUnit *get() const {
581 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
583 operator MDCompileUnit *() const { return get(); }
584 MDCompileUnit *operator->() const { return get(); }
585 MDCompileUnit &operator*() const {
586 assert(get() && "Expected valid pointer");
590 dwarf::SourceLanguage getLanguage() const {
591 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
593 StringRef getProducer() const { return get()->getProducer(); }
594 bool isOptimized() const { return get()->isOptimized(); }
595 StringRef getFlags() const { return get()->getFlags(); }
596 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
598 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
599 DIArray getRetainedTypes() const {
600 return DIArray(get()->getRetainedTypes());
602 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
603 DIArray getGlobalVariables() const {
604 return DIArray(get()->getGlobalVariables());
606 DIArray getImportedEntities() const {
607 return DIArray(get()->getImportedEntities());
610 void replaceSubprograms(DIArray Subprograms);
611 void replaceGlobalVariables(DIArray GlobalVariables);
613 StringRef getSplitDebugFilename() const {
614 return get()->getSplitDebugFilename();
616 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
621 /// \brief This is a wrapper for a subprogram (e.g. a function).
622 class DISubprogram : public DIScope {
624 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
625 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
627 MDSubprogram *get() const {
628 return cast_or_null<MDSubprogram>(DIDescriptor::get());
630 operator MDSubprogram *() const { return get(); }
631 MDSubprogram *operator->() const { return get(); }
632 MDSubprogram &operator*() const {
633 assert(get() && "Expected valid pointer");
637 StringRef getName() const { return get()->getName(); }
638 StringRef getDisplayName() const { return get()->getDisplayName(); }
639 StringRef getLinkageName() const { return get()->getLinkageName(); }
640 unsigned getLineNumber() const { return get()->getLine(); }
642 /// \brief Check if this is local (like 'static' in C).
643 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
644 unsigned isDefinition() const { return get()->isDefinition(); }
646 unsigned getVirtuality() const { return get()->getVirtuality(); }
647 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
649 unsigned getFlags() const { return get()->getFlags(); }
651 unsigned isOptimized() const { return get()->isOptimized(); }
653 /// \brief Get the beginning of the scope of the function (not the name).
654 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
656 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
657 DISubroutineType getType() const {
658 return DISubroutineType(get()->getType());
661 DITypeRef getContainingType() const {
662 return DITypeRef::get(get()->getContainingType());
667 /// \brief Check if this provides debugging information for the function F.
668 bool describes(const Function *F);
670 Function *getFunction() const;
672 void replaceFunction(Function *F) {
674 N->replaceFunction(F);
676 DIArray getTemplateParams() const {
677 return DIArray(get()->getTemplateParams());
679 DISubprogram getFunctionDeclaration() const {
680 return DISubprogram(get()->getDeclaration());
682 MDNode *getVariablesNodes() const { return getVariables(); }
683 DIArray getVariables() const { return DIArray(get()->getVariables()); }
685 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
686 /// \brief Check for the "private" access specifier.
687 bool isPrivate() const {
688 return (getFlags() & FlagAccessibility) == FlagPrivate;
690 /// \brief Check for the "protected" access specifier.
691 bool isProtected() const {
692 return (getFlags() & FlagAccessibility) == FlagProtected;
694 /// \brief Check for the "public" access specifier.
695 bool isPublic() const {
696 return (getFlags() & FlagAccessibility) == FlagPublic;
698 /// \brief Check for "explicit".
699 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
700 /// \brief Check if this is prototyped.
701 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
703 /// \brief Check if this is reference-qualified.
705 /// Return true if this subprogram is a C++11 reference-qualified non-static
706 /// member function (void foo() &).
707 unsigned isLValueReference() const {
708 return (getFlags() & FlagLValueReference) != 0;
711 /// \brief Check if this is rvalue-reference-qualified.
713 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
714 /// non-static member function (void foo() &&).
715 unsigned isRValueReference() const {
716 return (getFlags() & FlagRValueReference) != 0;
720 /// \brief This is a wrapper for a lexical block.
721 class DILexicalBlock : public DIScope {
723 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
724 DILexicalBlock(const MDLexicalBlock *N) : DIScope(N) {}
726 MDLexicalBlockBase *get() const {
727 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
729 operator MDLexicalBlockBase *() const { return get(); }
730 MDLexicalBlockBase *operator->() const { return get(); }
731 MDLexicalBlockBase &operator*() const {
732 assert(get() && "Expected valid pointer");
736 DIScope getContext() const { return DIScope(get()->getScope()); }
737 unsigned getLineNumber() const {
738 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
742 unsigned getColumnNumber() const {
743 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
744 return N->getColumn();
750 /// \brief This is a wrapper for a lexical block with a filename change.
751 class DILexicalBlockFile : public DIScope {
753 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
754 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
756 MDLexicalBlockFile *get() const {
757 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
759 operator MDLexicalBlockFile *() const { return get(); }
760 MDLexicalBlockFile *operator->() const { return get(); }
761 MDLexicalBlockFile &operator*() const {
762 assert(get() && "Expected valid pointer");
766 DIScope getContext() const { return getScope(); }
767 unsigned getLineNumber() const { return getScope().getLineNumber(); }
768 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
769 DILexicalBlock getScope() const { return DILexicalBlock(get()->getScope()); }
770 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
774 /// \brief A wrapper for a C++ style name space.
775 class DINameSpace : public DIScope {
777 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
778 DINameSpace(const MDNamespace *N) : DIScope(N) {}
780 MDNamespace *get() const {
781 return cast_or_null<MDNamespace>(DIDescriptor::get());
783 operator MDNamespace *() const { return get(); }
784 MDNamespace *operator->() const { return get(); }
785 MDNamespace &operator*() const {
786 assert(get() && "Expected valid pointer");
790 StringRef getName() const { return get()->getName(); }
791 unsigned getLineNumber() const { return get()->getLine(); }
792 DIScope getContext() const { return DIScope(get()->getScope()); }
796 /// \brief This is a wrapper for template type parameter.
797 class DITemplateTypeParameter : public DIDescriptor {
799 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
801 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
803 MDTemplateTypeParameter *get() const {
804 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
806 operator MDTemplateTypeParameter *() const { return get(); }
807 MDTemplateTypeParameter *operator->() const { return get(); }
808 MDTemplateTypeParameter &operator*() const {
809 assert(get() && "Expected valid pointer");
813 StringRef getName() const { return get()->getName(); }
815 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
819 /// \brief This is a wrapper for template value parameter.
820 class DITemplateValueParameter : public DIDescriptor {
822 explicit DITemplateValueParameter(const MDNode *N = nullptr)
824 DITemplateValueParameter(const MDTemplateValueParameter *N)
827 MDTemplateValueParameter *get() const {
828 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
830 operator MDTemplateValueParameter *() const { return get(); }
831 MDTemplateValueParameter *operator->() const { return get(); }
832 MDTemplateValueParameter &operator*() const {
833 assert(get() && "Expected valid pointer");
837 StringRef getName() const { return get()->getName(); }
838 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
839 Metadata *getValue() const { return get()->getValue(); }
843 /// \brief This is a wrapper for a global variable.
844 class DIGlobalVariable : public DIDescriptor {
845 DIFile getFile() const { return DIFile(get()->getFile()); }
848 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
849 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
851 MDGlobalVariable *get() const {
852 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
854 operator MDGlobalVariable *() const { return get(); }
855 MDGlobalVariable *operator->() const { return get(); }
856 MDGlobalVariable &operator*() const {
857 assert(get() && "Expected valid pointer");
861 StringRef getName() const { return get()->getName(); }
862 StringRef getDisplayName() const { return get()->getDisplayName(); }
863 StringRef getLinkageName() const { return get()->getLinkageName(); }
864 unsigned getLineNumber() const { return get()->getLine(); }
865 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
866 unsigned isDefinition() const { return get()->isDefinition(); }
868 DIScope getContext() const { return DIScope(get()->getScope()); }
869 StringRef getFilename() const { return getFile().getFilename(); }
870 StringRef getDirectory() const { return getFile().getDirectory(); }
871 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
873 GlobalVariable *getGlobal() const;
874 Constant *getConstant() const {
876 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
877 return C->getValue();
880 DIDerivedType getStaticDataMemberDeclaration() const {
881 return DIDerivedType(get()->getStaticDataMemberDeclaration());
887 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
888 class DIVariable : public DIDescriptor {
889 unsigned getFlags() const { return get()->getFlags(); }
892 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
893 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
895 MDLocalVariable *get() const {
896 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
898 operator MDLocalVariable *() const { return get(); }
899 MDLocalVariable *operator->() const { return get(); }
900 MDLocalVariable &operator*() const {
901 assert(get() && "Expected valid pointer");
905 StringRef getName() const { return get()->getName(); }
906 unsigned getLineNumber() const { return get()->getLine(); }
907 unsigned getArgNumber() const { return get()->getArg(); }
909 DIScope getContext() const { return DIScope(get()->getScope()); }
910 DIFile getFile() const { return DIFile(get()->getFile()); }
911 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
913 /// \brief Return true if this variable is marked as "artificial".
914 bool isArtificial() const {
915 return (getFlags() & FlagArtificial) != 0;
918 bool isObjectPointer() const {
919 return (getFlags() & FlagObjectPointer) != 0;
922 /// \brief If this variable is inlined then return inline location.
923 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
927 /// \brief Check if this is a "__block" variable (Apple Blocks).
928 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
929 return (getType().resolve(Map)).isBlockByrefStruct();
932 /// \brief Check if this is an inlined function argument.
933 bool isInlinedFnArgument(const Function *CurFn);
935 /// \brief Return the size reported by the variable's type.
936 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
938 void printExtendedName(raw_ostream &OS) const;
941 /// \brief A complex location expression in postfix notation.
943 /// This is (almost) a DWARF expression that modifies the location of a
944 /// variable or (or the location of a single piece of a variable).
946 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
947 /// and have DW_OP_plus consume the topmost elements on the stack.
948 class DIExpression : public DIDescriptor {
950 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
951 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
953 MDExpression *get() const {
954 return cast_or_null<MDExpression>(DIDescriptor::get());
956 operator MDExpression *() const { return get(); }
957 MDExpression *operator->() const { return get(); }
958 MDExpression &operator*() const {
959 assert(get() && "Expected valid pointer");
963 // Don't call this. Call isValid() directly.
964 bool Verify() const = delete;
966 /// \brief Return the number of elements in the complex expression.
967 unsigned getNumElements() const { return get()->getNumElements(); }
969 /// \brief return the Idx'th complex address element.
970 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
972 /// \brief Return whether this is a piece of an aggregate variable.
973 bool isBitPiece() const;
974 /// \brief Return the offset of this piece in bits.
975 uint64_t getBitPieceOffset() const;
976 /// \brief Return the size of this piece in bits.
977 uint64_t getBitPieceSize() const;
980 /// \brief A lightweight wrapper around an element of a DIExpression.
982 friend class iterator;
983 MDExpression::element_iterator I;
985 Operand(MDExpression::element_iterator I) : I(I) {}
987 /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
988 /// Argument 0 is the operand itself.
989 uint64_t getArg(unsigned N) const {
990 MDExpression::element_iterator In = I;
994 operator uint64_t () const { return *I; }
995 /// \brief Returns underlying MDExpression::element_iterator.
996 const MDExpression::element_iterator &getBase() const { return I; }
997 /// \brief Returns the next operand.
998 iterator getNext() const;
1001 /// \brief An iterator for DIExpression elements.
1002 class iterator : public std::iterator<std::input_iterator_tag, StringRef,
1003 unsigned, const Operand*, Operand> {
1004 friend class Operand;
1005 MDExpression::element_iterator I;
1009 iterator(MDExpression::element_iterator I) : I(I) {}
1010 const Operand &operator*() { return Tmp = Operand(I); }
1011 const Operand *operator->() { return &(Tmp = Operand(I)); }
1012 iterator &operator++() {
1016 iterator operator++(int) {
1021 bool operator==(const iterator &X) const { return I == X.I; }
1022 bool operator!=(const iterator &X) const { return !(*this == X); }
1027 case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
1028 case dwarf::DW_OP_plus: std::advance(I, 2); break;
1029 case dwarf::DW_OP_deref: std::advance(I, 1); break;
1031 llvm_unreachable("unsupported operand");
1036 iterator begin() const { return get()->elements_begin(); }
1037 iterator end() const { return get()->elements_end(); }
1040 /// \brief This object holds location information.
1042 /// This object is not associated with any DWARF tag.
1043 class DILocation : public DIDescriptor {
1045 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
1046 DILocation(const MDLocation *N) : DIDescriptor(N) {}
1048 MDLocation *get() const {
1049 return cast_or_null<MDLocation>(DIDescriptor::get());
1051 operator MDLocation *() const { return get(); }
1052 MDLocation *operator->() const { return get(); }
1053 MDLocation &operator*() const {
1054 assert(get() && "Expected valid pointer");
1058 unsigned getLineNumber() const { return get()->getLine(); }
1059 unsigned getColumnNumber() const { return get()->getColumn(); }
1060 DIScope getScope() const { return DIScope(get()->getScope()); }
1061 DILocation getOrigLocation() const {
1062 return DILocation(get()->getInlinedAt());
1064 StringRef getFilename() const { return getScope().getFilename(); }
1065 StringRef getDirectory() const { return getScope().getDirectory(); }
1066 bool Verify() const;
1067 bool atSameLineAs(const DILocation &Other) const {
1068 return (getLineNumber() == Other.getLineNumber() &&
1069 getFilename() == Other.getFilename());
1071 /// \brief Get the DWAF discriminator.
1073 /// DWARF discriminators are used to distinguish identical file locations for
1074 /// instructions that are on different basic blocks. If two instructions are
1075 /// inside the same lexical block and are in different basic blocks, we
1076 /// create a new lexical block with identical location as the original but
1077 /// with a different discriminator value
1078 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1079 unsigned getDiscriminator() const {
1080 // Since discriminators are associated with lexical blocks, make
1081 // sure this location is a lexical block before retrieving its
1083 return getScope().isLexicalBlockFile()
1084 ? DILexicalBlockFile(
1085 cast<MDNode>(cast<MDLocation>(DbgNode)->getScope()))
1090 /// \brief Generate a new discriminator value for this location.
1091 unsigned computeNewDiscriminator(LLVMContext &Ctx);
1093 /// \brief Return a copy of this location with a different scope.
1094 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1097 class DIObjCProperty : public DIDescriptor {
1099 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
1100 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1102 MDObjCProperty *get() const {
1103 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1105 operator MDObjCProperty *() const { return get(); }
1106 MDObjCProperty *operator->() const { return get(); }
1107 MDObjCProperty &operator*() const {
1108 assert(get() && "Expected valid pointer");
1112 StringRef getObjCPropertyName() const { return get()->getName(); }
1113 DIFile getFile() const { return DIFile(get()->getFile()); }
1114 unsigned getLineNumber() const { return get()->getLine(); }
1116 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1117 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1118 unsigned getAttributes() const { return get()->getAttributes(); }
1119 bool isReadOnlyObjCProperty() const {
1120 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1122 bool isReadWriteObjCProperty() const {
1123 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1125 bool isAssignObjCProperty() const {
1126 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1128 bool isRetainObjCProperty() const {
1129 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1131 bool isCopyObjCProperty() const {
1132 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1134 bool isNonAtomicObjCProperty() const {
1135 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1138 /// \brief Get the type.
1140 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1141 /// the type as a DITypeRef here.
1142 DIType getType() const { return DIType(get()->getType()); }
1144 bool Verify() const;
1147 /// \brief An imported module (C++ using directive or similar).
1148 class DIImportedEntity : public DIDescriptor {
1150 DIImportedEntity() = default;
1151 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
1152 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1154 MDImportedEntity *get() const {
1155 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1157 operator MDImportedEntity *() const { return get(); }
1158 MDImportedEntity *operator->() const { return get(); }
1159 MDImportedEntity &operator*() const {
1160 assert(get() && "Expected valid pointer");
1164 DIScope getContext() const { return DIScope(get()->getScope()); }
1165 DIDescriptorRef getEntity() const {
1166 return DIDescriptorRef::get(get()->getEntity());
1168 unsigned getLineNumber() const { return get()->getLine(); }
1169 StringRef getName() const { return get()->getName(); }
1170 bool Verify() const;
1173 /// \brief Find subprogram that is enclosing this scope.
1174 DISubprogram getDISubprogram(const MDNode *Scope);
1176 /// \brief Find debug info for a given function.
1177 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1179 DISubprogram getDISubprogram(const Function *F);
1181 /// \brief Find underlying composite type.
1182 DICompositeType getDICompositeType(DIType T);
1184 /// \brief Create a new inlined variable based on current variable.
1186 /// @param DV Current Variable.
1187 /// @param InlinedScope Location at current variable is inlined.
1188 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1189 LLVMContext &VMContext);
1191 /// \brief Remove inlined scope from the variable.
1192 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1194 /// \brief Generate map by visiting all retained types.
1195 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1197 /// \brief Strip debug info in the module if it exists.
1199 /// To do this, we remove all calls to the debugger intrinsics and any named
1200 /// metadata for debugging. We also remove debug locations for instructions.
1201 /// Return true if module is modified.
1202 bool StripDebugInfo(Module &M);
1203 bool stripDebugInfo(Function &F);
1205 /// \brief Return Debug Info Metadata Version by checking module flags.
1206 unsigned getDebugMetadataVersionFromModule(const Module &M);
1208 /// \brief Utility to find all debug info in a module.
1210 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1211 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1212 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1213 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1214 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1215 /// used by the CUs.
1216 class DebugInfoFinder {
1218 DebugInfoFinder() : TypeMapInitialized(false) {}
1220 /// \brief Process entire module and collect debug info anchors.
1221 void processModule(const Module &M);
1223 /// \brief Process DbgDeclareInst.
1224 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1225 /// \brief Process DbgValueInst.
1226 void processValue(const Module &M, const DbgValueInst *DVI);
1227 /// \brief Process DILocation.
1228 void processLocation(const Module &M, DILocation Loc);
1230 /// \brief Clear all lists.
1234 void InitializeTypeMap(const Module &M);
1236 void processType(DIType DT);
1237 void processSubprogram(DISubprogram SP);
1238 void processScope(DIScope Scope);
1239 bool addCompileUnit(DICompileUnit CU);
1240 bool addGlobalVariable(DIGlobalVariable DIG);
1241 bool addSubprogram(DISubprogram SP);
1242 bool addType(DIType DT);
1243 bool addScope(DIScope Scope);
1246 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1247 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1248 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1249 global_variable_iterator;
1250 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1251 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1253 iterator_range<compile_unit_iterator> compile_units() const {
1254 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1257 iterator_range<subprogram_iterator> subprograms() const {
1258 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1261 iterator_range<global_variable_iterator> global_variables() const {
1262 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1265 iterator_range<type_iterator> types() const {
1266 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1269 iterator_range<scope_iterator> scopes() const {
1270 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1273 unsigned compile_unit_count() const { return CUs.size(); }
1274 unsigned global_variable_count() const { return GVs.size(); }
1275 unsigned subprogram_count() const { return SPs.size(); }
1276 unsigned type_count() const { return TYs.size(); }
1277 unsigned scope_count() const { return Scopes.size(); }
1280 SmallVector<DICompileUnit, 8> CUs;
1281 SmallVector<DISubprogram, 8> SPs;
1282 SmallVector<DIGlobalVariable, 8> GVs;
1283 SmallVector<DIType, 8> TYs;
1284 SmallVector<DIScope, 8> Scopes;
1285 SmallPtrSet<MDNode *, 64> NodesSeen;
1286 DITypeIdentifierMap TypeIdentifierMap;
1288 /// \brief Specify if TypeIdentifierMap is initialized.
1289 bool TypeMapInitialized;
1292 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1294 } // end namespace llvm