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 unsigned getNumElements() const {
199 return DbgNode ? DbgNode->getNumOperands() : 0;
201 T getElement(unsigned Idx) const { return getFieldAs<T>(Idx); }
204 typedef DITypedArray<DIDescriptor> DIArray;
206 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
208 /// FIXME: it seems strange that this doesn't have either a reference to the
209 /// type/precision or a file/line pair for location info.
210 class DIEnumerator : public DIDescriptor {
212 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
213 DIEnumerator(const MDEnumerator *N) : DIDescriptor(N) {}
215 MDEnumerator *get() const {
216 return cast_or_null<MDEnumerator>(DIDescriptor::get());
218 operator MDEnumerator *() const { return get(); }
219 MDEnumerator *operator->() const { return get(); }
220 MDEnumerator &operator*() const {
221 assert(get() && "Expected valid pointer");
225 StringRef getName() const { return get()->getName(); }
226 int64_t getEnumValue() const { return get()->getValue(); }
230 template <typename T> class DIRef;
231 typedef DIRef<DIDescriptor> DIDescriptorRef;
232 typedef DIRef<DIScope> DIScopeRef;
233 typedef DIRef<DIType> DITypeRef;
234 typedef DITypedArray<DITypeRef> DITypeArray;
236 /// \brief A base class for various scopes.
238 /// Although, implementation-wise, DIScope is the parent class of most
239 /// other DIxxx classes, including DIType and its descendants, most of
240 /// DIScope's descendants are not a substitutable subtype of
241 /// DIScope. The DIDescriptor::isScope() method only is true for
242 /// DIScopes that are scopes in the strict lexical scope sense
243 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
244 class DIScope : public DIDescriptor {
246 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
247 DIScope(const MDScope *N) : DIDescriptor(N) {}
249 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
250 operator MDScope *() const { return get(); }
251 MDScope *operator->() const { return get(); }
252 MDScope &operator*() const {
253 assert(get() && "Expected valid pointer");
257 /// \brief Get the parent scope.
259 /// Gets the parent scope for this scope node or returns a default
260 /// constructed scope.
261 DIScopeRef getContext() const;
262 /// \brief Get the scope name.
264 /// If the scope node has a name, return that, else return an empty string.
265 StringRef getName() const;
266 StringRef getFilename() const;
267 StringRef getDirectory() const;
269 /// \brief Generate a reference to this DIScope.
271 /// Uses the type identifier instead of the actual MDNode if possible, to
272 /// help type uniquing.
273 DIScopeRef getRef() const;
276 /// \brief Represents reference to a DIDescriptor.
278 /// Abstracts over direct and identifier-based metadata references.
279 template <typename T> class DIRef {
280 template <typename DescTy>
281 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
282 friend DIScopeRef DIScope::getContext() const;
283 friend DIScopeRef DIScope::getRef() const;
286 /// \brief Val can be either a MDNode or a MDString.
288 /// In the latter, MDString specifies the type identifier.
290 explicit DIRef(const Metadata *V);
294 DIRef(const TypedDebugNodeRef<U> &Ref,
295 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
299 T resolve(const DITypeIdentifierMap &Map) const;
300 operator Metadata *() const { return const_cast<Metadata *>(Val); }
302 static DIRef get(const Metadata *MD) { return DIRef(MD); }
305 template <typename T>
306 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
310 if (const MDNode *MD = dyn_cast<MDNode>(Val))
313 const MDString *MS = cast<MDString>(Val);
314 // Find the corresponding MDNode.
315 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
316 assert(Iter != Map.end() && "Identifier not in the type map?");
317 assert(DIDescriptor(Iter->second).isType() &&
318 "MDNode in DITypeIdentifierMap should be a DIType.");
319 return T(Iter->second);
322 /// \brief Handle fields that are references to DIDescriptors.
324 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
325 /// \brief Specialize DIRef constructor for DIDescriptorRef.
326 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
328 /// \brief Handle fields that are references to DIScopes.
329 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
330 /// \brief Specialize DIRef constructor for DIScopeRef.
331 template <> DIRef<DIScope>::DIRef(const Metadata *V);
333 /// \brief Handle fields that are references to DITypes.
334 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
335 /// \brief Specialize DIRef constructor for DITypeRef.
336 template <> DIRef<DIType>::DIRef(const Metadata *V);
338 /// \brief This is a wrapper for a type.
340 /// FIXME: Types should be factored much better so that CV qualifiers and
341 /// others do not require a huge and empty descriptor full of zeros.
342 class DIType : public DIScope {
344 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
345 DIType(const MDType *N) : DIScope(N) {}
347 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
348 operator MDType *() const { return get(); }
349 MDType *operator->() const { return get(); }
350 MDType &operator*() const {
351 assert(get() && "Expected valid pointer");
355 operator DITypeRef() const {
357 "constructing DITypeRef from an MDNode that is not a type");
358 return DITypeRef(&*getRef());
363 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
364 StringRef getName() const { return get()->getName(); }
365 unsigned getLineNumber() const { return get()->getLine(); }
366 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
367 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
368 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
369 // carry this is just plain insane.
370 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
371 unsigned getFlags() const { return get()->getFlags(); }
372 bool isPrivate() const {
373 return (getFlags() & FlagAccessibility) == FlagPrivate;
375 bool isProtected() const {
376 return (getFlags() & FlagAccessibility) == FlagProtected;
378 bool isPublic() const {
379 return (getFlags() & FlagAccessibility) == FlagPublic;
381 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
382 bool isAppleBlockExtension() const {
383 return (getFlags() & FlagAppleBlock) != 0;
385 bool isBlockByrefStruct() const {
386 return (getFlags() & FlagBlockByrefStruct) != 0;
388 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
389 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
390 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
391 bool isObjcClassComplete() const {
392 return (getFlags() & FlagObjcClassComplete) != 0;
394 bool isVector() const { return (getFlags() & FlagVector) != 0; }
395 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
396 bool isLValueReference() const {
397 return (getFlags() & FlagLValueReference) != 0;
399 bool isRValueReference() const {
400 return (getFlags() & FlagRValueReference) != 0;
402 bool isValid() const { return DbgNode && isType(); }
405 /// \brief A basic type, like 'int' or 'float'.
406 class DIBasicType : public DIType {
408 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
409 DIBasicType(const MDBasicType *N) : DIType(N) {}
411 MDBasicType *get() const {
412 return cast_or_null<MDBasicType>(DIDescriptor::get());
414 operator MDBasicType *() const { return get(); }
415 MDBasicType *operator->() const { return get(); }
416 MDBasicType &operator*() const {
417 assert(get() && "Expected valid pointer");
421 unsigned getEncoding() const { return get()->getEncoding(); }
426 /// \brief A simple derived type
428 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
429 /// Or, a data member of a class/struct/union.
430 class DIDerivedType : public DIType {
432 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
433 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
435 MDDerivedTypeBase *get() const {
436 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
438 operator MDDerivedTypeBase *() const { return get(); }
439 MDDerivedTypeBase *operator->() const { return get(); }
440 MDDerivedTypeBase &operator*() const {
441 assert(get() && "Expected valid pointer");
445 DITypeRef getTypeDerivedFrom() const {
446 return DITypeRef::get(get()->getBaseType());
449 /// \brief Return property node, if this ivar is associated with one.
450 MDNode *getObjCProperty() const {
451 if (auto *N = dyn_cast<MDDerivedType>(get()))
452 return dyn_cast_or_null<MDNode>(N->getExtraData());
456 DITypeRef getClassType() const {
457 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
458 if (auto *N = dyn_cast<MDDerivedType>(get()))
459 return DITypeRef::get(N->getExtraData());
460 return DITypeRef::get(nullptr);
463 Constant *getConstant() const {
464 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
465 if (auto *N = dyn_cast<MDDerivedType>(get()))
466 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
467 return C->getValue();
475 /// \brief Types that refer to multiple other types.
477 /// This descriptor holds a type that can refer to multiple other types, like a
478 /// function or struct.
480 /// DICompositeType is derived from DIDerivedType because some
481 /// composite types (such as enums) can be derived from basic types
482 // FIXME: Make this derive from DIType directly & just store the
483 // base type in a single DIType field.
484 class DICompositeType : public DIDerivedType {
485 friend class DIBuilder;
487 /// \brief Set the array of member DITypes.
488 void setArraysHelper(MDNode *Elements, MDNode *TParams);
491 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
492 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
494 MDCompositeTypeBase *get() const {
495 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
497 operator MDCompositeTypeBase *() const { return get(); }
498 MDCompositeTypeBase *operator->() const { return get(); }
499 MDCompositeTypeBase &operator*() const {
500 assert(get() && "Expected valid pointer");
504 DIArray getElements() const {
505 assert(!isSubroutineType() && "no elements for DISubroutineType");
506 return DIArray(get()->getElements());
510 template <typename T>
511 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
513 (!TParams || DbgNode->getNumOperands() == 8) &&
514 "If you're setting the template parameters this should include a slot "
516 setArraysHelper(Elements, TParams);
520 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
521 DITypeRef getContainingType() const {
522 return DITypeRef::get(get()->getVTableHolder());
526 /// \brief Set the containing type.
527 void setContainingType(DICompositeType ContainingType);
530 DIArray getTemplateParams() const {
531 return DIArray(get()->getTemplateParams());
533 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
538 class DISubroutineType : public DICompositeType {
540 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
541 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
543 MDSubroutineType *get() const {
544 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
546 operator MDSubroutineType *() const { return get(); }
547 MDSubroutineType *operator->() const { return get(); }
548 MDSubroutineType &operator*() const {
549 assert(get() && "Expected valid pointer");
553 DITypedArray<DITypeRef> getTypeArray() const {
554 return DITypedArray<DITypeRef>(get()->getTypeArray());
558 /// \brief This is a wrapper for a file.
559 class DIFile : public DIScope {
561 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
562 DIFile(const MDFile *N) : DIScope(N) {}
564 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
565 operator MDFile *() const { return get(); }
566 MDFile *operator->() const { return get(); }
567 MDFile &operator*() const {
568 assert(get() && "Expected valid pointer");
572 /// \brief Retrieve the MDNode for the directory/file pair.
573 MDNode *getFileNode() const { return get(); }
577 /// \brief A wrapper for a compile unit.
578 class DICompileUnit : public DIScope {
580 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
581 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
583 MDCompileUnit *get() const {
584 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
586 operator MDCompileUnit *() const { return get(); }
587 MDCompileUnit *operator->() const { return get(); }
588 MDCompileUnit &operator*() const {
589 assert(get() && "Expected valid pointer");
593 dwarf::SourceLanguage getLanguage() const {
594 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
596 StringRef getProducer() const { return get()->getProducer(); }
597 bool isOptimized() const { return get()->isOptimized(); }
598 StringRef getFlags() const { return get()->getFlags(); }
599 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
601 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
602 DIArray getRetainedTypes() const {
603 return DIArray(get()->getRetainedTypes());
605 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
606 DIArray getGlobalVariables() const {
607 return DIArray(get()->getGlobalVariables());
609 DIArray getImportedEntities() const {
610 return DIArray(get()->getImportedEntities());
613 void replaceSubprograms(DIArray Subprograms);
614 void replaceGlobalVariables(DIArray GlobalVariables);
616 StringRef getSplitDebugFilename() const {
617 return get()->getSplitDebugFilename();
619 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
624 /// \brief This is a wrapper for a subprogram (e.g. a function).
625 class DISubprogram : public DIScope {
627 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
628 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
630 MDSubprogram *get() const {
631 return cast_or_null<MDSubprogram>(DIDescriptor::get());
633 operator MDSubprogram *() const { return get(); }
634 MDSubprogram *operator->() const { return get(); }
635 MDSubprogram &operator*() const {
636 assert(get() && "Expected valid pointer");
640 StringRef getName() const { return get()->getName(); }
641 StringRef getDisplayName() const { return get()->getDisplayName(); }
642 StringRef getLinkageName() const { return get()->getLinkageName(); }
643 unsigned getLineNumber() const { return get()->getLine(); }
645 /// \brief Check if this is local (like 'static' in C).
646 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
647 unsigned isDefinition() const { return get()->isDefinition(); }
649 unsigned getVirtuality() const { return get()->getVirtuality(); }
650 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
652 unsigned getFlags() const { return get()->getFlags(); }
654 unsigned isOptimized() const { return get()->isOptimized(); }
656 /// \brief Get the beginning of the scope of the function (not the name).
657 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
659 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
660 DISubroutineType getType() const {
661 return DISubroutineType(get()->getType());
664 DITypeRef getContainingType() const {
665 return DITypeRef::get(get()->getContainingType());
670 /// \brief Check if this provides debugging information for the function F.
671 bool describes(const Function *F);
673 Function *getFunction() const;
675 void replaceFunction(Function *F) {
677 N->replaceFunction(F);
679 DIArray getTemplateParams() const {
680 return DIArray(get()->getTemplateParams());
682 DISubprogram getFunctionDeclaration() const {
683 return DISubprogram(get()->getDeclaration());
685 MDNode *getVariablesNodes() const { return getVariables(); }
686 DIArray getVariables() const { return DIArray(get()->getVariables()); }
688 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
689 /// \brief Check for the "private" access specifier.
690 bool isPrivate() const {
691 return (getFlags() & FlagAccessibility) == FlagPrivate;
693 /// \brief Check for the "protected" access specifier.
694 bool isProtected() const {
695 return (getFlags() & FlagAccessibility) == FlagProtected;
697 /// \brief Check for the "public" access specifier.
698 bool isPublic() const {
699 return (getFlags() & FlagAccessibility) == FlagPublic;
701 /// \brief Check for "explicit".
702 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
703 /// \brief Check if this is prototyped.
704 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
706 /// \brief Check if this is reference-qualified.
708 /// Return true if this subprogram is a C++11 reference-qualified non-static
709 /// member function (void foo() &).
710 unsigned isLValueReference() const {
711 return (getFlags() & FlagLValueReference) != 0;
714 /// \brief Check if this is rvalue-reference-qualified.
716 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
717 /// non-static member function (void foo() &&).
718 unsigned isRValueReference() const {
719 return (getFlags() & FlagRValueReference) != 0;
723 /// \brief This is a wrapper for a lexical block.
724 class DILexicalBlock : public DIScope {
726 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
727 DILexicalBlock(const MDLexicalBlock *N) : DIScope(N) {}
729 MDLexicalBlockBase *get() const {
730 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
732 operator MDLexicalBlockBase *() const { return get(); }
733 MDLexicalBlockBase *operator->() const { return get(); }
734 MDLexicalBlockBase &operator*() const {
735 assert(get() && "Expected valid pointer");
739 DIScope getContext() const { return DIScope(get()->getScope()); }
740 unsigned getLineNumber() const {
741 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
745 unsigned getColumnNumber() const {
746 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
747 return N->getColumn();
753 /// \brief This is a wrapper for a lexical block with a filename change.
754 class DILexicalBlockFile : public DIScope {
756 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
757 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
759 MDLexicalBlockFile *get() const {
760 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
762 operator MDLexicalBlockFile *() const { return get(); }
763 MDLexicalBlockFile *operator->() const { return get(); }
764 MDLexicalBlockFile &operator*() const {
765 assert(get() && "Expected valid pointer");
769 DIScope getContext() const { return getScope(); }
770 unsigned getLineNumber() const { return getScope().getLineNumber(); }
771 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
772 DILexicalBlock getScope() const { return DILexicalBlock(get()->getScope()); }
773 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
777 /// \brief A wrapper for a C++ style name space.
778 class DINameSpace : public DIScope {
780 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
781 DINameSpace(const MDNamespace *N) : DIScope(N) {}
783 MDNamespace *get() const {
784 return cast_or_null<MDNamespace>(DIDescriptor::get());
786 operator MDNamespace *() const { return get(); }
787 MDNamespace *operator->() const { return get(); }
788 MDNamespace &operator*() const {
789 assert(get() && "Expected valid pointer");
793 StringRef getName() const { return get()->getName(); }
794 unsigned getLineNumber() const { return get()->getLine(); }
795 DIScope getContext() const { return DIScope(get()->getScope()); }
799 /// \brief This is a wrapper for template type parameter.
800 class DITemplateTypeParameter : public DIDescriptor {
802 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
804 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
806 MDTemplateTypeParameter *get() const {
807 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
809 operator MDTemplateTypeParameter *() const { return get(); }
810 MDTemplateTypeParameter *operator->() const { return get(); }
811 MDTemplateTypeParameter &operator*() const {
812 assert(get() && "Expected valid pointer");
816 StringRef getName() const { return get()->getName(); }
818 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
822 /// \brief This is a wrapper for template value parameter.
823 class DITemplateValueParameter : public DIDescriptor {
825 explicit DITemplateValueParameter(const MDNode *N = nullptr)
827 DITemplateValueParameter(const MDTemplateValueParameter *N)
830 MDTemplateValueParameter *get() const {
831 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
833 operator MDTemplateValueParameter *() const { return get(); }
834 MDTemplateValueParameter *operator->() const { return get(); }
835 MDTemplateValueParameter &operator*() const {
836 assert(get() && "Expected valid pointer");
840 StringRef getName() const { return get()->getName(); }
841 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
842 Metadata *getValue() const { return get()->getValue(); }
846 /// \brief This is a wrapper for a global variable.
847 class DIGlobalVariable : public DIDescriptor {
848 DIFile getFile() const { return DIFile(get()->getFile()); }
851 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
852 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
854 MDGlobalVariable *get() const {
855 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
857 operator MDGlobalVariable *() const { return get(); }
858 MDGlobalVariable *operator->() const { return get(); }
859 MDGlobalVariable &operator*() const {
860 assert(get() && "Expected valid pointer");
864 StringRef getName() const { return get()->getName(); }
865 StringRef getDisplayName() const { return get()->getDisplayName(); }
866 StringRef getLinkageName() const { return get()->getLinkageName(); }
867 unsigned getLineNumber() const { return get()->getLine(); }
868 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
869 unsigned isDefinition() const { return get()->isDefinition(); }
871 DIScope getContext() const { return DIScope(get()->getScope()); }
872 StringRef getFilename() const { return getFile().getFilename(); }
873 StringRef getDirectory() const { return getFile().getDirectory(); }
874 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
876 GlobalVariable *getGlobal() const;
877 Constant *getConstant() const {
879 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
880 return C->getValue();
883 DIDerivedType getStaticDataMemberDeclaration() const {
884 return DIDerivedType(get()->getStaticDataMemberDeclaration());
890 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
891 class DIVariable : public DIDescriptor {
892 unsigned getFlags() const { return get()->getFlags(); }
895 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
896 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
898 MDLocalVariable *get() const {
899 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
901 operator MDLocalVariable *() const { return get(); }
902 MDLocalVariable *operator->() const { return get(); }
903 MDLocalVariable &operator*() const {
904 assert(get() && "Expected valid pointer");
908 StringRef getName() const { return get()->getName(); }
909 unsigned getLineNumber() const { return get()->getLine(); }
910 unsigned getArgNumber() const { return get()->getArg(); }
912 DIScope getContext() const { return DIScope(get()->getScope()); }
913 DIFile getFile() const { return DIFile(get()->getFile()); }
914 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
916 /// \brief Return true if this variable is marked as "artificial".
917 bool isArtificial() const {
918 return (getFlags() & FlagArtificial) != 0;
921 bool isObjectPointer() const {
922 return (getFlags() & FlagObjectPointer) != 0;
925 /// \brief If this variable is inlined then return inline location.
926 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
930 /// \brief Check if this is a "__block" variable (Apple Blocks).
931 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
932 return (getType().resolve(Map)).isBlockByrefStruct();
935 /// \brief Check if this is an inlined function argument.
936 bool isInlinedFnArgument(const Function *CurFn);
938 /// \brief Return the size reported by the variable's type.
939 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
941 void printExtendedName(raw_ostream &OS) const;
944 /// \brief A complex location expression in postfix notation.
946 /// This is (almost) a DWARF expression that modifies the location of a
947 /// variable or (or the location of a single piece of a variable).
949 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
950 /// and have DW_OP_plus consume the topmost elements on the stack.
951 class DIExpression : public DIDescriptor {
953 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
954 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
956 MDExpression *get() const {
957 return cast_or_null<MDExpression>(DIDescriptor::get());
959 operator MDExpression *() const { return get(); }
960 MDExpression *operator->() const { return get(); }
961 MDExpression &operator*() const {
962 assert(get() && "Expected valid pointer");
966 // Don't call this. Call isValid() directly.
967 bool Verify() const = delete;
969 /// \brief Return the number of elements in the complex expression.
970 unsigned getNumElements() const { return get()->getNumElements(); }
972 /// \brief return the Idx'th complex address element.
973 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
975 /// \brief Return whether this is a piece of an aggregate variable.
976 bool isBitPiece() const;
977 /// \brief Return the offset of this piece in bits.
978 uint64_t getBitPieceOffset() const;
979 /// \brief Return the size of this piece in bits.
980 uint64_t getBitPieceSize() const;
983 /// \brief A lightweight wrapper around an element of a DIExpression.
985 friend class iterator;
986 MDExpression::element_iterator I;
988 Operand(MDExpression::element_iterator I) : I(I) {}
990 /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
991 /// Argument 0 is the operand itself.
992 uint64_t getArg(unsigned N) const {
993 MDExpression::element_iterator In = I;
997 operator uint64_t () const { return *I; }
998 /// \brief Returns underlying MDExpression::element_iterator.
999 const MDExpression::element_iterator &getBase() const { return I; }
1000 /// \brief Returns the next operand.
1001 iterator getNext() const;
1004 /// \brief An iterator for DIExpression elements.
1005 class iterator : public std::iterator<std::input_iterator_tag, StringRef,
1006 unsigned, const Operand*, Operand> {
1007 friend class Operand;
1008 MDExpression::element_iterator I;
1012 iterator(MDExpression::element_iterator I) : I(I) {}
1013 const Operand &operator*() { return Tmp = Operand(I); }
1014 const Operand *operator->() { return &(Tmp = Operand(I)); }
1015 iterator &operator++() {
1019 iterator operator++(int) {
1024 bool operator==(const iterator &X) const { return I == X.I; }
1025 bool operator!=(const iterator &X) const { return !(*this == X); }
1030 case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
1031 case dwarf::DW_OP_plus: std::advance(I, 2); break;
1032 case dwarf::DW_OP_deref: std::advance(I, 1); break;
1034 llvm_unreachable("unsupported operand");
1039 iterator begin() const { return get()->elements_begin(); }
1040 iterator end() const { return get()->elements_end(); }
1043 /// \brief This object holds location information.
1045 /// This object is not associated with any DWARF tag.
1046 class DILocation : public DIDescriptor {
1048 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
1049 DILocation(const MDLocation *N) : DIDescriptor(N) {}
1051 MDLocation *get() const {
1052 return cast_or_null<MDLocation>(DIDescriptor::get());
1054 operator MDLocation *() const { return get(); }
1055 MDLocation *operator->() const { return get(); }
1056 MDLocation &operator*() const {
1057 assert(get() && "Expected valid pointer");
1061 unsigned getLineNumber() const { return get()->getLine(); }
1062 unsigned getColumnNumber() const { return get()->getColumn(); }
1063 DIScope getScope() const { return DIScope(get()->getScope()); }
1064 DILocation getOrigLocation() const {
1065 return DILocation(get()->getInlinedAt());
1067 StringRef getFilename() const { return getScope().getFilename(); }
1068 StringRef getDirectory() const { return getScope().getDirectory(); }
1069 bool Verify() const;
1070 bool atSameLineAs(const DILocation &Other) const {
1071 return (getLineNumber() == Other.getLineNumber() &&
1072 getFilename() == Other.getFilename());
1074 /// \brief Get the DWAF discriminator.
1076 /// DWARF discriminators are used to distinguish identical file locations for
1077 /// instructions that are on different basic blocks. If two instructions are
1078 /// inside the same lexical block and are in different basic blocks, we
1079 /// create a new lexical block with identical location as the original but
1080 /// with a different discriminator value
1081 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1082 unsigned getDiscriminator() const {
1083 // Since discriminators are associated with lexical blocks, make
1084 // sure this location is a lexical block before retrieving its
1086 return getScope().isLexicalBlockFile()
1087 ? DILexicalBlockFile(
1088 cast<MDNode>(cast<MDLocation>(DbgNode)->getScope()))
1093 /// \brief Generate a new discriminator value for this location.
1094 unsigned computeNewDiscriminator(LLVMContext &Ctx);
1096 /// \brief Return a copy of this location with a different scope.
1097 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1100 class DIObjCProperty : public DIDescriptor {
1102 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
1103 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1105 MDObjCProperty *get() const {
1106 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1108 operator MDObjCProperty *() const { return get(); }
1109 MDObjCProperty *operator->() const { return get(); }
1110 MDObjCProperty &operator*() const {
1111 assert(get() && "Expected valid pointer");
1115 StringRef getObjCPropertyName() const { return get()->getName(); }
1116 DIFile getFile() const { return DIFile(get()->getFile()); }
1117 unsigned getLineNumber() const { return get()->getLine(); }
1119 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1120 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1121 unsigned getAttributes() const { return get()->getAttributes(); }
1122 bool isReadOnlyObjCProperty() const {
1123 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1125 bool isReadWriteObjCProperty() const {
1126 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1128 bool isAssignObjCProperty() const {
1129 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1131 bool isRetainObjCProperty() const {
1132 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1134 bool isCopyObjCProperty() const {
1135 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1137 bool isNonAtomicObjCProperty() const {
1138 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1141 /// \brief Get the type.
1143 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1144 /// the type as a DITypeRef here.
1145 DIType getType() const { return DIType(get()->getType()); }
1147 bool Verify() const;
1150 /// \brief An imported module (C++ using directive or similar).
1151 class DIImportedEntity : public DIDescriptor {
1153 DIImportedEntity() = default;
1154 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
1155 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1157 MDImportedEntity *get() const {
1158 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1160 operator MDImportedEntity *() const { return get(); }
1161 MDImportedEntity *operator->() const { return get(); }
1162 MDImportedEntity &operator*() const {
1163 assert(get() && "Expected valid pointer");
1167 DIScope getContext() const { return DIScope(get()->getScope()); }
1168 DIDescriptorRef getEntity() const {
1169 return DIDescriptorRef::get(get()->getEntity());
1171 unsigned getLineNumber() const { return get()->getLine(); }
1172 StringRef getName() const { return get()->getName(); }
1173 bool Verify() const;
1176 /// \brief Find subprogram that is enclosing this scope.
1177 DISubprogram getDISubprogram(const MDNode *Scope);
1179 /// \brief Find debug info for a given function.
1180 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1182 DISubprogram getDISubprogram(const Function *F);
1184 /// \brief Find underlying composite type.
1185 DICompositeType getDICompositeType(DIType T);
1187 /// \brief Create a new inlined variable based on current variable.
1189 /// @param DV Current Variable.
1190 /// @param InlinedScope Location at current variable is inlined.
1191 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1192 LLVMContext &VMContext);
1194 /// \brief Remove inlined scope from the variable.
1195 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1197 /// \brief Generate map by visiting all retained types.
1198 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1200 /// \brief Strip debug info in the module if it exists.
1202 /// To do this, we remove all calls to the debugger intrinsics and any named
1203 /// metadata for debugging. We also remove debug locations for instructions.
1204 /// Return true if module is modified.
1205 bool StripDebugInfo(Module &M);
1206 bool stripDebugInfo(Function &F);
1208 /// \brief Return Debug Info Metadata Version by checking module flags.
1209 unsigned getDebugMetadataVersionFromModule(const Module &M);
1211 /// \brief Utility to find all debug info in a module.
1213 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1214 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1215 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1216 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1217 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1218 /// used by the CUs.
1219 class DebugInfoFinder {
1221 DebugInfoFinder() : TypeMapInitialized(false) {}
1223 /// \brief Process entire module and collect debug info anchors.
1224 void processModule(const Module &M);
1226 /// \brief Process DbgDeclareInst.
1227 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1228 /// \brief Process DbgValueInst.
1229 void processValue(const Module &M, const DbgValueInst *DVI);
1230 /// \brief Process DILocation.
1231 void processLocation(const Module &M, DILocation Loc);
1233 /// \brief Clear all lists.
1237 void InitializeTypeMap(const Module &M);
1239 void processType(DIType DT);
1240 void processSubprogram(DISubprogram SP);
1241 void processScope(DIScope Scope);
1242 bool addCompileUnit(DICompileUnit CU);
1243 bool addGlobalVariable(DIGlobalVariable DIG);
1244 bool addSubprogram(DISubprogram SP);
1245 bool addType(DIType DT);
1246 bool addScope(DIScope Scope);
1249 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1250 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1251 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1252 global_variable_iterator;
1253 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1254 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1256 iterator_range<compile_unit_iterator> compile_units() const {
1257 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1260 iterator_range<subprogram_iterator> subprograms() const {
1261 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1264 iterator_range<global_variable_iterator> global_variables() const {
1265 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1268 iterator_range<type_iterator> types() const {
1269 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1272 iterator_range<scope_iterator> scopes() const {
1273 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1276 unsigned compile_unit_count() const { return CUs.size(); }
1277 unsigned global_variable_count() const { return GVs.size(); }
1278 unsigned subprogram_count() const { return SPs.size(); }
1279 unsigned type_count() const { return TYs.size(); }
1280 unsigned scope_count() const { return Scopes.size(); }
1283 SmallVector<DICompileUnit, 8> CUs;
1284 SmallVector<DISubprogram, 8> SPs;
1285 SmallVector<DIGlobalVariable, 8> GVs;
1286 SmallVector<DIType, 8> TYs;
1287 SmallVector<DIScope, 8> Scopes;
1288 SmallPtrSet<MDNode *, 64> NodesSeen;
1289 DITypeIdentifierMap TypeIdentifierMap;
1291 /// \brief Specify if TypeIdentifierMap is initialized.
1292 bool TypeMapInitialized;
1295 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1297 } // end namespace llvm