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");
358 operator DITypeRef() const {
360 "constructing DITypeRef from an MDNode that is not a type");
361 return DITypeRef(&*getRef());
366 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
367 StringRef getName() const { return get()->getName(); }
368 unsigned getLineNumber() const { return get()->getLine(); }
369 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
370 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
371 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
372 // carry this is just plain insane.
373 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
374 unsigned getFlags() const { return get()->getFlags(); }
375 bool isPrivate() const {
376 return (getFlags() & FlagAccessibility) == FlagPrivate;
378 bool isProtected() const {
379 return (getFlags() & FlagAccessibility) == FlagProtected;
381 bool isPublic() const {
382 return (getFlags() & FlagAccessibility) == FlagPublic;
384 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
385 bool isAppleBlockExtension() const {
386 return (getFlags() & FlagAppleBlock) != 0;
388 bool isBlockByrefStruct() const {
389 return (getFlags() & FlagBlockByrefStruct) != 0;
391 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
392 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
393 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
394 bool isObjcClassComplete() const {
395 return (getFlags() & FlagObjcClassComplete) != 0;
397 bool isVector() const { return (getFlags() & FlagVector) != 0; }
398 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
399 bool isLValueReference() const {
400 return (getFlags() & FlagLValueReference) != 0;
402 bool isRValueReference() const {
403 return (getFlags() & FlagRValueReference) != 0;
405 bool isValid() const { return DbgNode && isType(); }
408 /// \brief A basic type, like 'int' or 'float'.
409 class DIBasicType : public DIType {
411 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
412 DIBasicType(const MDBasicType *N) : DIType(N) {}
414 MDBasicType *get() const {
415 return cast_or_null<MDBasicType>(DIDescriptor::get());
417 operator MDBasicType *() const { return get(); }
418 MDBasicType *operator->() const { return get(); }
419 MDBasicType &operator*() const {
420 assert(get() && "Expected valid pointer");
424 unsigned getEncoding() const { return get()->getEncoding(); }
429 /// \brief A simple derived type
431 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
432 /// Or, a data member of a class/struct/union.
433 class DIDerivedType : public DIType {
435 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
436 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
438 MDDerivedTypeBase *get() const {
439 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
441 operator MDDerivedTypeBase *() const { return get(); }
442 MDDerivedTypeBase *operator->() const { return get(); }
443 MDDerivedTypeBase &operator*() const {
444 assert(get() && "Expected valid pointer");
448 DITypeRef getTypeDerivedFrom() const {
449 return DITypeRef::get(get()->getBaseType());
452 /// \brief Return property node, if this ivar is associated with one.
453 MDNode *getObjCProperty() const {
454 if (auto *N = dyn_cast<MDDerivedType>(get()))
455 return dyn_cast_or_null<MDNode>(N->getExtraData());
459 DITypeRef getClassType() const {
460 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
461 if (auto *N = dyn_cast<MDDerivedType>(get()))
462 return DITypeRef::get(N->getExtraData());
463 return DITypeRef::get(nullptr);
466 Constant *getConstant() const {
467 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
468 if (auto *N = dyn_cast<MDDerivedType>(get()))
469 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
470 return C->getValue();
478 /// \brief Types that refer to multiple other types.
480 /// This descriptor holds a type that can refer to multiple other types, like a
481 /// function or struct.
483 /// DICompositeType is derived from DIDerivedType because some
484 /// composite types (such as enums) can be derived from basic types
485 // FIXME: Make this derive from DIType directly & just store the
486 // base type in a single DIType field.
487 class DICompositeType : public DIDerivedType {
488 friend class DIBuilder;
490 /// \brief Set the array of member DITypes.
491 void setArraysHelper(MDNode *Elements, MDNode *TParams);
494 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
495 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
497 MDCompositeTypeBase *get() const {
498 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
500 operator MDCompositeTypeBase *() const { return get(); }
501 MDCompositeTypeBase *operator->() const { return get(); }
502 MDCompositeTypeBase &operator*() const {
503 assert(get() && "Expected valid pointer");
507 DIArray getElements() const {
508 assert(!isSubroutineType() && "no elements for DISubroutineType");
509 return DIArray(get()->getElements());
513 template <typename T>
514 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
516 (!TParams || DbgNode->getNumOperands() == 8) &&
517 "If you're setting the template parameters this should include a slot "
519 setArraysHelper(Elements, TParams);
523 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
524 DITypeRef getContainingType() const {
525 return DITypeRef::get(get()->getVTableHolder());
529 /// \brief Set the containing type.
530 void setContainingType(DICompositeType ContainingType);
533 DIArray getTemplateParams() const {
534 return DIArray(get()->getTemplateParams());
536 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
541 class DISubroutineType : public DICompositeType {
543 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
544 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
546 MDSubroutineType *get() const {
547 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
549 operator MDSubroutineType *() const { return get(); }
550 MDSubroutineType *operator->() const { return get(); }
551 MDSubroutineType &operator*() const {
552 assert(get() && "Expected valid pointer");
556 DITypedArray<DITypeRef> getTypeArray() const {
557 return DITypedArray<DITypeRef>(get()->getTypeArray());
561 /// \brief This is a wrapper for a file.
562 class DIFile : public DIScope {
564 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
565 DIFile(const MDFile *N) : DIScope(N) {}
567 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
568 operator MDFile *() const { return get(); }
569 MDFile *operator->() const { return get(); }
570 MDFile &operator*() const {
571 assert(get() && "Expected valid pointer");
575 /// \brief Retrieve the MDNode for the directory/file pair.
576 MDNode *getFileNode() const { return get(); }
580 /// \brief A wrapper for a compile unit.
581 class DICompileUnit : public DIScope {
583 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
584 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
586 MDCompileUnit *get() const {
587 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
589 operator MDCompileUnit *() const { return get(); }
590 MDCompileUnit *operator->() const { return get(); }
591 MDCompileUnit &operator*() const {
592 assert(get() && "Expected valid pointer");
596 dwarf::SourceLanguage getLanguage() const {
597 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
599 StringRef getProducer() const { return get()->getProducer(); }
600 bool isOptimized() const { return get()->isOptimized(); }
601 StringRef getFlags() const { return get()->getFlags(); }
602 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
604 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
605 DIArray getRetainedTypes() const {
606 return DIArray(get()->getRetainedTypes());
608 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
609 DIArray getGlobalVariables() const {
610 return DIArray(get()->getGlobalVariables());
612 DIArray getImportedEntities() const {
613 return DIArray(get()->getImportedEntities());
616 void replaceSubprograms(DIArray Subprograms);
617 void replaceGlobalVariables(DIArray GlobalVariables);
619 StringRef getSplitDebugFilename() const {
620 return get()->getSplitDebugFilename();
622 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
627 /// \brief This is a wrapper for a subprogram (e.g. a function).
628 class DISubprogram : public DIScope {
630 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
631 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
633 MDSubprogram *get() const {
634 return cast_or_null<MDSubprogram>(DIDescriptor::get());
636 operator MDSubprogram *() const { return get(); }
637 MDSubprogram *operator->() const { return get(); }
638 MDSubprogram &operator*() const {
639 assert(get() && "Expected valid pointer");
643 StringRef getName() const { return get()->getName(); }
644 StringRef getDisplayName() const { return get()->getDisplayName(); }
645 StringRef getLinkageName() const { return get()->getLinkageName(); }
646 unsigned getLineNumber() const { return get()->getLine(); }
648 /// \brief Check if this is local (like 'static' in C).
649 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
650 unsigned isDefinition() const { return get()->isDefinition(); }
652 unsigned getVirtuality() const { return get()->getVirtuality(); }
653 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
655 unsigned getFlags() const { return get()->getFlags(); }
657 unsigned isOptimized() const { return get()->isOptimized(); }
659 /// \brief Get the beginning of the scope of the function (not the name).
660 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
662 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
663 DISubroutineType getType() const {
664 return DISubroutineType(get()->getType());
667 DITypeRef getContainingType() const {
668 return DITypeRef::get(get()->getContainingType());
673 /// \brief Check if this provides debugging information for the function F.
674 bool describes(const Function *F);
676 Function *getFunction() const;
678 void replaceFunction(Function *F) {
680 N->replaceFunction(F);
682 DIArray getTemplateParams() const {
683 return DIArray(get()->getTemplateParams());
685 DISubprogram getFunctionDeclaration() const {
686 return DISubprogram(get()->getDeclaration());
688 MDNode *getVariablesNodes() const { return getVariables(); }
689 DIArray getVariables() const { return DIArray(get()->getVariables()); }
691 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
692 /// \brief Check for the "private" access specifier.
693 bool isPrivate() const {
694 return (getFlags() & FlagAccessibility) == FlagPrivate;
696 /// \brief Check for the "protected" access specifier.
697 bool isProtected() const {
698 return (getFlags() & FlagAccessibility) == FlagProtected;
700 /// \brief Check for the "public" access specifier.
701 bool isPublic() const {
702 return (getFlags() & FlagAccessibility) == FlagPublic;
704 /// \brief Check for "explicit".
705 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
706 /// \brief Check if this is prototyped.
707 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
709 /// \brief Check if this is reference-qualified.
711 /// Return true if this subprogram is a C++11 reference-qualified non-static
712 /// member function (void foo() &).
713 unsigned isLValueReference() const {
714 return (getFlags() & FlagLValueReference) != 0;
717 /// \brief Check if this is rvalue-reference-qualified.
719 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
720 /// non-static member function (void foo() &&).
721 unsigned isRValueReference() const {
722 return (getFlags() & FlagRValueReference) != 0;
726 /// \brief This is a wrapper for a lexical block.
727 class DILexicalBlock : public DIScope {
729 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
730 DILexicalBlock(const MDLexicalBlock *N) : DIScope(N) {}
732 MDLexicalBlockBase *get() const {
733 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
735 operator MDLexicalBlockBase *() const { return get(); }
736 MDLexicalBlockBase *operator->() const { return get(); }
737 MDLexicalBlockBase &operator*() const {
738 assert(get() && "Expected valid pointer");
742 DIScope getContext() const { return DIScope(get()->getScope()); }
743 unsigned getLineNumber() const {
744 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
748 unsigned getColumnNumber() const {
749 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
750 return N->getColumn();
756 /// \brief This is a wrapper for a lexical block with a filename change.
757 class DILexicalBlockFile : public DIScope {
759 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
760 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
762 MDLexicalBlockFile *get() const {
763 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
765 operator MDLexicalBlockFile *() const { return get(); }
766 MDLexicalBlockFile *operator->() const { return get(); }
767 MDLexicalBlockFile &operator*() const {
768 assert(get() && "Expected valid pointer");
772 DIScope getContext() const { return getScope(); }
773 unsigned getLineNumber() const { return getScope().getLineNumber(); }
774 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
775 DILexicalBlock getScope() const { return DILexicalBlock(get()->getScope()); }
776 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
780 /// \brief A wrapper for a C++ style name space.
781 class DINameSpace : public DIScope {
783 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
784 DINameSpace(const MDNamespace *N) : DIScope(N) {}
786 MDNamespace *get() const {
787 return cast_or_null<MDNamespace>(DIDescriptor::get());
789 operator MDNamespace *() const { return get(); }
790 MDNamespace *operator->() const { return get(); }
791 MDNamespace &operator*() const {
792 assert(get() && "Expected valid pointer");
796 StringRef getName() const { return get()->getName(); }
797 unsigned getLineNumber() const { return get()->getLine(); }
798 DIScope getContext() const { return DIScope(get()->getScope()); }
802 /// \brief This is a wrapper for template type parameter.
803 class DITemplateTypeParameter : public DIDescriptor {
805 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
807 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
809 MDTemplateTypeParameter *get() const {
810 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
812 operator MDTemplateTypeParameter *() const { return get(); }
813 MDTemplateTypeParameter *operator->() const { return get(); }
814 MDTemplateTypeParameter &operator*() const {
815 assert(get() && "Expected valid pointer");
819 StringRef getName() const { return get()->getName(); }
821 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
825 /// \brief This is a wrapper for template value parameter.
826 class DITemplateValueParameter : public DIDescriptor {
828 explicit DITemplateValueParameter(const MDNode *N = nullptr)
830 DITemplateValueParameter(const MDTemplateValueParameter *N)
833 MDTemplateValueParameter *get() const {
834 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
836 operator MDTemplateValueParameter *() const { return get(); }
837 MDTemplateValueParameter *operator->() const { return get(); }
838 MDTemplateValueParameter &operator*() const {
839 assert(get() && "Expected valid pointer");
843 StringRef getName() const { return get()->getName(); }
844 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
845 Metadata *getValue() const { return get()->getValue(); }
849 /// \brief This is a wrapper for a global variable.
850 class DIGlobalVariable : public DIDescriptor {
851 DIFile getFile() const { return DIFile(get()->getFile()); }
854 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
855 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
857 MDGlobalVariable *get() const {
858 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
860 operator MDGlobalVariable *() const { return get(); }
861 MDGlobalVariable *operator->() const { return get(); }
862 MDGlobalVariable &operator*() const {
863 assert(get() && "Expected valid pointer");
867 StringRef getName() const { return get()->getName(); }
868 StringRef getDisplayName() const { return get()->getDisplayName(); }
869 StringRef getLinkageName() const { return get()->getLinkageName(); }
870 unsigned getLineNumber() const { return get()->getLine(); }
871 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
872 unsigned isDefinition() const { return get()->isDefinition(); }
874 DIScope getContext() const { return DIScope(get()->getScope()); }
875 StringRef getFilename() const { return getFile().getFilename(); }
876 StringRef getDirectory() const { return getFile().getDirectory(); }
877 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
879 GlobalVariable *getGlobal() const;
880 Constant *getConstant() const {
882 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
883 return C->getValue();
886 DIDerivedType getStaticDataMemberDeclaration() const {
887 return DIDerivedType(get()->getStaticDataMemberDeclaration());
893 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
894 class DIVariable : public DIDescriptor {
895 unsigned getFlags() const { return get()->getFlags(); }
898 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
899 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
901 MDLocalVariable *get() const {
902 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
904 operator MDLocalVariable *() const { return get(); }
905 MDLocalVariable *operator->() const { return get(); }
906 MDLocalVariable &operator*() const {
907 assert(get() && "Expected valid pointer");
911 StringRef getName() const { return get()->getName(); }
912 unsigned getLineNumber() const { return get()->getLine(); }
913 unsigned getArgNumber() const { return get()->getArg(); }
915 DIScope getContext() const { return DIScope(get()->getScope()); }
916 DIFile getFile() const { return DIFile(get()->getFile()); }
917 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
919 /// \brief Return true if this variable is marked as "artificial".
920 bool isArtificial() const {
921 return (getFlags() & FlagArtificial) != 0;
924 bool isObjectPointer() const {
925 return (getFlags() & FlagObjectPointer) != 0;
928 /// \brief If this variable is inlined then return inline location.
929 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
933 /// \brief Check if this is a "__block" variable (Apple Blocks).
934 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
935 return (getType().resolve(Map)).isBlockByrefStruct();
938 /// \brief Check if this is an inlined function argument.
939 bool isInlinedFnArgument(const Function *CurFn);
941 /// \brief Return the size reported by the variable's type.
942 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
944 void printExtendedName(raw_ostream &OS) const;
947 /// \brief A complex location expression in postfix notation.
949 /// This is (almost) a DWARF expression that modifies the location of a
950 /// variable or (or the location of a single piece of a variable).
952 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
953 /// and have DW_OP_plus consume the topmost elements on the stack.
954 class DIExpression : public DIDescriptor {
956 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
957 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
959 MDExpression *get() const {
960 return cast_or_null<MDExpression>(DIDescriptor::get());
962 operator MDExpression *() const { return get(); }
963 MDExpression *operator->() const { return get(); }
964 MDExpression &operator*() const {
965 assert(get() && "Expected valid pointer");
969 // Don't call this. Call isValid() directly.
970 bool Verify() const = delete;
972 /// \brief Return the number of elements in the complex expression.
973 unsigned getNumElements() const { return get()->getNumElements(); }
975 /// \brief return the Idx'th complex address element.
976 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
978 /// \brief Return whether this is a piece of an aggregate variable.
979 bool isBitPiece() const;
980 /// \brief Return the offset of this piece in bits.
981 uint64_t getBitPieceOffset() const;
982 /// \brief Return the size of this piece in bits.
983 uint64_t getBitPieceSize() const;
986 /// \brief A lightweight wrapper around an element of a DIExpression.
988 friend class iterator;
989 MDExpression::element_iterator I;
991 Operand(MDExpression::element_iterator I) : I(I) {}
993 /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
994 /// Argument 0 is the operand itself.
995 uint64_t getArg(unsigned N) const {
996 MDExpression::element_iterator In = I;
1000 operator uint64_t () const { return *I; }
1001 /// \brief Returns underlying MDExpression::element_iterator.
1002 const MDExpression::element_iterator &getBase() const { return I; }
1003 /// \brief Returns the next operand.
1004 iterator getNext() const;
1007 /// \brief An iterator for DIExpression elements.
1008 class iterator : public std::iterator<std::input_iterator_tag, StringRef,
1009 unsigned, const Operand*, Operand> {
1010 friend class Operand;
1011 MDExpression::element_iterator I;
1015 iterator(MDExpression::element_iterator I) : I(I) {}
1016 const Operand &operator*() { return Tmp = Operand(I); }
1017 const Operand *operator->() { return &(Tmp = Operand(I)); }
1018 iterator &operator++() {
1022 iterator operator++(int) {
1027 bool operator==(const iterator &X) const { return I == X.I; }
1028 bool operator!=(const iterator &X) const { return !(*this == X); }
1033 case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
1034 case dwarf::DW_OP_plus: std::advance(I, 2); break;
1035 case dwarf::DW_OP_deref: std::advance(I, 1); break;
1037 llvm_unreachable("unsupported operand");
1042 iterator begin() const { return get()->elements_begin(); }
1043 iterator end() const { return get()->elements_end(); }
1046 /// \brief This object holds location information.
1048 /// This object is not associated with any DWARF tag.
1049 class DILocation : public DIDescriptor {
1051 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
1052 DILocation(const MDLocation *N) : DIDescriptor(N) {}
1054 MDLocation *get() const {
1055 return cast_or_null<MDLocation>(DIDescriptor::get());
1057 operator MDLocation *() const { return get(); }
1058 MDLocation *operator->() const { return get(); }
1059 MDLocation &operator*() const {
1060 assert(get() && "Expected valid pointer");
1064 unsigned getLineNumber() const { return get()->getLine(); }
1065 unsigned getColumnNumber() const { return get()->getColumn(); }
1066 DIScope getScope() const { return DIScope(get()->getScope()); }
1067 DILocation getOrigLocation() const {
1068 return DILocation(get()->getInlinedAt());
1070 StringRef getFilename() const { return getScope().getFilename(); }
1071 StringRef getDirectory() const { return getScope().getDirectory(); }
1072 bool Verify() const;
1073 bool atSameLineAs(const DILocation &Other) const {
1074 return (getLineNumber() == Other.getLineNumber() &&
1075 getFilename() == Other.getFilename());
1077 /// \brief Get the DWAF discriminator.
1079 /// DWARF discriminators are used to distinguish identical file locations for
1080 /// instructions that are on different basic blocks. If two instructions are
1081 /// inside the same lexical block and are in different basic blocks, we
1082 /// create a new lexical block with identical location as the original but
1083 /// with a different discriminator value
1084 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1085 unsigned getDiscriminator() const {
1086 // Since discriminators are associated with lexical blocks, make
1087 // sure this location is a lexical block before retrieving its
1089 return getScope().isLexicalBlockFile()
1090 ? DILexicalBlockFile(
1091 cast<MDNode>(cast<MDLocation>(DbgNode)->getScope()))
1096 /// \brief Generate a new discriminator value for this location.
1097 unsigned computeNewDiscriminator(LLVMContext &Ctx);
1099 /// \brief Return a copy of this location with a different scope.
1100 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1103 class DIObjCProperty : public DIDescriptor {
1105 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
1106 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1108 MDObjCProperty *get() const {
1109 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1111 operator MDObjCProperty *() const { return get(); }
1112 MDObjCProperty *operator->() const { return get(); }
1113 MDObjCProperty &operator*() const {
1114 assert(get() && "Expected valid pointer");
1118 StringRef getObjCPropertyName() const { return get()->getName(); }
1119 DIFile getFile() const { return DIFile(get()->getFile()); }
1120 unsigned getLineNumber() const { return get()->getLine(); }
1122 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1123 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1124 unsigned getAttributes() const { return get()->getAttributes(); }
1125 bool isReadOnlyObjCProperty() const {
1126 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1128 bool isReadWriteObjCProperty() const {
1129 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1131 bool isAssignObjCProperty() const {
1132 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1134 bool isRetainObjCProperty() const {
1135 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1137 bool isCopyObjCProperty() const {
1138 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1140 bool isNonAtomicObjCProperty() const {
1141 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1144 /// \brief Get the type.
1146 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1147 /// the type as a DITypeRef here.
1148 DIType getType() const { return DIType(get()->getType()); }
1150 bool Verify() const;
1153 /// \brief An imported module (C++ using directive or similar).
1154 class DIImportedEntity : public DIDescriptor {
1156 DIImportedEntity() = default;
1157 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
1158 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1160 MDImportedEntity *get() const {
1161 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1163 operator MDImportedEntity *() const { return get(); }
1164 MDImportedEntity *operator->() const { return get(); }
1165 MDImportedEntity &operator*() const {
1166 assert(get() && "Expected valid pointer");
1170 DIScope getContext() const { return DIScope(get()->getScope()); }
1171 DIDescriptorRef getEntity() const {
1172 return DIDescriptorRef::get(get()->getEntity());
1174 unsigned getLineNumber() const { return get()->getLine(); }
1175 StringRef getName() const { return get()->getName(); }
1176 bool Verify() const;
1179 /// \brief Find subprogram that is enclosing this scope.
1180 DISubprogram getDISubprogram(const MDNode *Scope);
1182 /// \brief Find debug info for a given function.
1183 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1185 DISubprogram getDISubprogram(const Function *F);
1187 /// \brief Find underlying composite type.
1188 DICompositeType getDICompositeType(DIType T);
1190 /// \brief Create a new inlined variable based on current variable.
1192 /// @param DV Current Variable.
1193 /// @param InlinedScope Location at current variable is inlined.
1194 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1195 LLVMContext &VMContext);
1197 /// \brief Remove inlined scope from the variable.
1198 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1200 /// \brief Generate map by visiting all retained types.
1201 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1203 /// \brief Strip debug info in the module if it exists.
1205 /// To do this, we remove all calls to the debugger intrinsics and any named
1206 /// metadata for debugging. We also remove debug locations for instructions.
1207 /// Return true if module is modified.
1208 bool StripDebugInfo(Module &M);
1209 bool stripDebugInfo(Function &F);
1211 /// \brief Return Debug Info Metadata Version by checking module flags.
1212 unsigned getDebugMetadataVersionFromModule(const Module &M);
1214 /// \brief Utility to find all debug info in a module.
1216 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1217 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1218 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1219 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1220 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1221 /// used by the CUs.
1222 class DebugInfoFinder {
1224 DebugInfoFinder() : TypeMapInitialized(false) {}
1226 /// \brief Process entire module and collect debug info anchors.
1227 void processModule(const Module &M);
1229 /// \brief Process DbgDeclareInst.
1230 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1231 /// \brief Process DbgValueInst.
1232 void processValue(const Module &M, const DbgValueInst *DVI);
1233 /// \brief Process DILocation.
1234 void processLocation(const Module &M, DILocation Loc);
1236 /// \brief Clear all lists.
1240 void InitializeTypeMap(const Module &M);
1242 void processType(DIType DT);
1243 void processSubprogram(DISubprogram SP);
1244 void processScope(DIScope Scope);
1245 bool addCompileUnit(DICompileUnit CU);
1246 bool addGlobalVariable(DIGlobalVariable DIG);
1247 bool addSubprogram(DISubprogram SP);
1248 bool addType(DIType DT);
1249 bool addScope(DIScope Scope);
1252 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1253 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1254 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1255 global_variable_iterator;
1256 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1257 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1259 iterator_range<compile_unit_iterator> compile_units() const {
1260 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1263 iterator_range<subprogram_iterator> subprograms() const {
1264 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1267 iterator_range<global_variable_iterator> global_variables() const {
1268 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1271 iterator_range<type_iterator> types() const {
1272 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1275 iterator_range<scope_iterator> scopes() const {
1276 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1279 unsigned compile_unit_count() const { return CUs.size(); }
1280 unsigned global_variable_count() const { return GVs.size(); }
1281 unsigned subprogram_count() const { return SPs.size(); }
1282 unsigned type_count() const { return TYs.size(); }
1283 unsigned scope_count() const { return Scopes.size(); }
1286 SmallVector<DICompileUnit, 8> CUs;
1287 SmallVector<DISubprogram, 8> SPs;
1288 SmallVector<DIGlobalVariable, 8> GVs;
1289 SmallVector<DIType, 8> TYs;
1290 SmallVector<DIScope, 8> Scopes;
1291 SmallPtrSet<MDNode *, 64> NodesSeen;
1292 DITypeIdentifierMap TypeIdentifierMap;
1294 /// \brief Specify if TypeIdentifierMap is initialized.
1295 bool TypeMapInitialized;
1298 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1300 } // end namespace llvm