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) {}
101 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
102 operator MDNode *() const { return get(); }
103 MDNode *operator->() const { return get(); }
104 MDNode &operator*() const {
105 assert(get() && "Expected valid pointer");
109 // An explicit operator bool so that we can do testing of DI values
111 // FIXME: This operator bool isn't actually protecting anything at the
112 // moment due to the conversion operator above making DIDescriptor nodes
113 // implicitly convertable to bool.
114 explicit operator bool() const { return DbgNode != nullptr; }
116 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
117 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
119 uint16_t getTag() const {
120 if (auto *N = dyn_cast_or_null<DebugNode>(get()))
125 bool isDerivedType() const { return get() && isa<MDDerivedTypeBase>(get()); }
126 bool isCompositeType() const {
127 return get() && isa<MDCompositeTypeBase>(get());
129 bool isSubroutineType() const {
130 return get() && isa<MDSubroutineType>(get());
132 bool isBasicType() const { return get() && isa<MDBasicType>(get()); }
133 bool isVariable() const { return get() && isa<MDLocalVariable>(get()); }
134 bool isSubprogram() const { return get() && isa<MDSubprogram>(get()); }
135 bool isGlobalVariable() const {
136 return get() && isa<MDGlobalVariable>(get());
138 bool isScope() const { return get() && isa<MDScope>(get()); }
139 bool isFile() const { return get() && isa<MDFile>(get()); }
140 bool isCompileUnit() const { return get() && isa<MDCompileUnit>(get()); }
141 bool isNameSpace() const{ return get() && isa<MDNamespace>(get()); }
142 bool isLexicalBlockFile() const {
143 return get() && isa<MDLexicalBlockFile>(get());
145 bool isLexicalBlock() const {
146 return get() && isa<MDLexicalBlockBase>(get());
148 bool isSubrange() const { return get() && isa<MDSubrange>(get()); }
149 bool isEnumerator() const { return get() && isa<MDEnumerator>(get()); }
150 bool isType() const { return get() && isa<MDType>(get()); }
151 bool isTemplateTypeParameter() const {
152 return get() && isa<MDTemplateTypeParameter>(get());
154 bool isTemplateValueParameter() const {
155 return get() && isa<MDTemplateValueParameter>(get());
157 bool isObjCProperty() const { return get() && isa<MDObjCProperty>(get()); }
158 bool isImportedEntity() const {
159 return get() && isa<MDImportedEntity>(get());
161 bool isExpression() const { return get() && isa<MDExpression>(get()); }
163 void print(raw_ostream &OS) const;
166 /// \brief Replace all uses of debug info referenced by this descriptor.
167 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
168 void replaceAllUsesWith(MDNode *D);
171 /// \brief This is used to represent ranges, for array bounds.
172 class DISubrange : public DIDescriptor {
174 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
175 DISubrange(const MDSubrange *N) : DIDescriptor(N) {}
177 MDSubrange *get() const {
178 return cast_or_null<MDSubrange>(DIDescriptor::get());
180 operator MDSubrange *() const { return get(); }
181 MDSubrange *operator->() const { return get(); }
182 MDSubrange &operator*() const {
183 assert(get() && "Expected valid pointer");
187 int64_t getLo() const { return get()->getLo(); }
188 int64_t getCount() const { return get()->getCount(); }
191 /// \brief This descriptor holds an array of nodes with type T.
192 template <typename T> class DITypedArray : public DIDescriptor {
194 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
195 operator MDTuple *() const {
196 return const_cast<MDTuple *>(cast_or_null<MDTuple>(DbgNode));
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(); }
229 template <typename T> class DIRef;
230 typedef DIRef<DIDescriptor> DIDescriptorRef;
231 typedef DIRef<DIScope> DIScopeRef;
232 typedef DIRef<DIType> DITypeRef;
233 typedef DITypedArray<DITypeRef> DITypeArray;
235 /// \brief A base class for various scopes.
237 /// Although, implementation-wise, DIScope is the parent class of most
238 /// other DIxxx classes, including DIType and its descendants, most of
239 /// DIScope's descendants are not a substitutable subtype of
240 /// DIScope. The DIDescriptor::isScope() method only is true for
241 /// DIScopes that are scopes in the strict lexical scope sense
242 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
243 class DIScope : public DIDescriptor {
245 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
246 DIScope(const MDScope *N) : DIDescriptor(N) {}
248 MDScope *get() const { return cast_or_null<MDScope>(DIDescriptor::get()); }
249 operator MDScope *() const { return get(); }
250 MDScope *operator->() const { return get(); }
251 MDScope &operator*() const {
252 assert(get() && "Expected valid pointer");
256 /// \brief Get the parent scope.
258 /// Gets the parent scope for this scope node or returns a default
259 /// constructed scope.
260 DIScopeRef getContext() const;
261 /// \brief Get the scope name.
263 /// If the scope node has a name, return that, else return an empty string.
264 StringRef getName() const;
265 StringRef getFilename() const;
266 StringRef getDirectory() const;
268 /// \brief Generate a reference to this DIScope.
270 /// Uses the type identifier instead of the actual MDNode if possible, to
271 /// help type uniquing.
272 DIScopeRef getRef() const;
275 /// \brief Represents reference to a DIDescriptor.
277 /// Abstracts over direct and identifier-based metadata references.
278 template <typename T> class DIRef {
279 template <typename DescTy>
280 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
281 friend DIScopeRef DIScope::getContext() const;
282 friend DIScopeRef DIScope::getRef() const;
285 /// \brief Val can be either a MDNode or a MDString.
287 /// In the latter, MDString specifies the type identifier.
289 explicit DIRef(const Metadata *V);
293 DIRef(const TypedDebugNodeRef<U> &Ref,
294 typename std::enable_if<std::is_convertible<U *, T>::value>::type * =
298 T resolve(const DITypeIdentifierMap &Map) const;
299 operator Metadata *() const { return const_cast<Metadata *>(Val); }
301 static DIRef get(const Metadata *MD) { return DIRef(MD); }
304 template <typename T>
305 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
309 if (const MDNode *MD = dyn_cast<MDNode>(Val))
312 const MDString *MS = cast<MDString>(Val);
313 // Find the corresponding MDNode.
314 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
315 assert(Iter != Map.end() && "Identifier not in the type map?");
316 assert(DIDescriptor(Iter->second).isType() &&
317 "MDNode in DITypeIdentifierMap should be a DIType.");
318 return T(Iter->second);
321 /// \brief Handle fields that are references to DIDescriptors.
323 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
324 /// \brief Specialize DIRef constructor for DIDescriptorRef.
325 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
327 /// \brief Handle fields that are references to DIScopes.
328 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
329 /// \brief Specialize DIRef constructor for DIScopeRef.
330 template <> DIRef<DIScope>::DIRef(const Metadata *V);
332 /// \brief Handle fields that are references to DITypes.
333 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
334 /// \brief Specialize DIRef constructor for DITypeRef.
335 template <> DIRef<DIType>::DIRef(const Metadata *V);
337 /// \brief This is a wrapper for a type.
339 /// FIXME: Types should be factored much better so that CV qualifiers and
340 /// others do not require a huge and empty descriptor full of zeros.
341 class DIType : public DIScope {
343 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
344 DIType(const MDType *N) : DIScope(N) {}
346 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
347 operator MDType *() const { return get(); }
348 MDType *operator->() const { return get(); }
349 MDType &operator*() const {
350 assert(get() && "Expected valid pointer");
354 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
355 StringRef getName() const { return get()->getName(); }
356 unsigned getLineNumber() const { return get()->getLine(); }
357 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
358 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
359 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
360 // carry this is just plain insane.
361 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
362 unsigned getFlags() const { return get()->getFlags(); }
363 bool isPrivate() const {
364 return (getFlags() & FlagAccessibility) == FlagPrivate;
366 bool isProtected() const {
367 return (getFlags() & FlagAccessibility) == FlagProtected;
369 bool isPublic() const {
370 return (getFlags() & FlagAccessibility) == FlagPublic;
372 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
373 bool isAppleBlockExtension() const {
374 return (getFlags() & FlagAppleBlock) != 0;
376 bool isBlockByrefStruct() const {
377 return (getFlags() & FlagBlockByrefStruct) != 0;
379 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
380 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
381 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
382 bool isObjcClassComplete() const {
383 return (getFlags() & FlagObjcClassComplete) != 0;
385 bool isVector() const { return (getFlags() & FlagVector) != 0; }
386 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
387 bool isLValueReference() const {
388 return (getFlags() & FlagLValueReference) != 0;
390 bool isRValueReference() const {
391 return (getFlags() & FlagRValueReference) != 0;
393 bool isValid() const { return DbgNode && isType(); }
396 /// \brief A basic type, like 'int' or 'float'.
397 class DIBasicType : public DIType {
399 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
400 DIBasicType(const MDBasicType *N) : DIType(N) {}
402 MDBasicType *get() const {
403 return cast_or_null<MDBasicType>(DIDescriptor::get());
405 operator MDBasicType *() const { return get(); }
406 MDBasicType *operator->() const { return get(); }
407 MDBasicType &operator*() const {
408 assert(get() && "Expected valid pointer");
412 unsigned getEncoding() const { return get()->getEncoding(); }
415 /// \brief A simple derived type
417 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
418 /// Or, a data member of a class/struct/union.
419 class DIDerivedType : public DIType {
421 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
422 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
424 MDDerivedTypeBase *get() const {
425 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
427 operator MDDerivedTypeBase *() const { return get(); }
428 MDDerivedTypeBase *operator->() const { return get(); }
429 MDDerivedTypeBase &operator*() const {
430 assert(get() && "Expected valid pointer");
434 DITypeRef getTypeDerivedFrom() const {
435 return DITypeRef::get(get()->getBaseType());
438 /// \brief Return property node, if this ivar is associated with one.
439 MDNode *getObjCProperty() const {
440 if (auto *N = dyn_cast<MDDerivedType>(get()))
441 return dyn_cast_or_null<MDNode>(N->getExtraData());
445 DITypeRef getClassType() const {
446 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
447 if (auto *N = dyn_cast<MDDerivedType>(get()))
448 return DITypeRef::get(N->getExtraData());
449 return DITypeRef::get(nullptr);
452 Constant *getConstant() const {
453 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
454 if (auto *N = dyn_cast<MDDerivedType>(get()))
455 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
456 return C->getValue();
462 /// \brief Types that refer to multiple other types.
464 /// This descriptor holds a type that can refer to multiple other types, like a
465 /// function or struct.
467 /// DICompositeType is derived from DIDerivedType because some
468 /// composite types (such as enums) can be derived from basic types
469 // FIXME: Make this derive from DIType directly & just store the
470 // base type in a single DIType field.
471 class DICompositeType : public DIDerivedType {
472 friend class DIBuilder;
474 /// \brief Set the array of member DITypes.
475 void setArraysHelper(MDNode *Elements, MDNode *TParams);
478 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
479 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
481 MDCompositeTypeBase *get() const {
482 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
484 operator MDCompositeTypeBase *() const { return get(); }
485 MDCompositeTypeBase *operator->() const { return get(); }
486 MDCompositeTypeBase &operator*() const {
487 assert(get() && "Expected valid pointer");
491 DIArray getElements() const {
492 assert(!isSubroutineType() && "no elements for DISubroutineType");
493 return DIArray(get()->getElements());
497 template <typename T>
498 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
500 (!TParams || DbgNode->getNumOperands() == 8) &&
501 "If you're setting the template parameters this should include a slot "
503 setArraysHelper(Elements, TParams);
507 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
508 DITypeRef getContainingType() const {
509 return DITypeRef::get(get()->getVTableHolder());
513 /// \brief Set the containing type.
514 void setContainingType(DICompositeType ContainingType);
517 DIArray getTemplateParams() const {
518 return DIArray(get()->getTemplateParams());
520 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
523 class DISubroutineType : public DICompositeType {
525 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
526 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
528 MDSubroutineType *get() const {
529 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
531 operator MDSubroutineType *() const { return get(); }
532 MDSubroutineType *operator->() const { return get(); }
533 MDSubroutineType &operator*() const {
534 assert(get() && "Expected valid pointer");
538 DITypedArray<DITypeRef> getTypeArray() const {
539 return DITypedArray<DITypeRef>(get()->getTypeArray());
543 /// \brief This is a wrapper for a file.
544 class DIFile : public DIScope {
546 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
547 DIFile(const MDFile *N) : DIScope(N) {}
549 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
550 operator MDFile *() const { return get(); }
551 MDFile *operator->() const { return get(); }
552 MDFile &operator*() const {
553 assert(get() && "Expected valid pointer");
557 /// \brief Retrieve the MDNode for the directory/file pair.
558 MDNode *getFileNode() const { return get(); }
561 /// \brief A wrapper for a compile unit.
562 class DICompileUnit : public DIScope {
564 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
565 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
567 MDCompileUnit *get() const {
568 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
570 operator MDCompileUnit *() const { return get(); }
571 MDCompileUnit *operator->() const { return get(); }
572 MDCompileUnit &operator*() const {
573 assert(get() && "Expected valid pointer");
577 dwarf::SourceLanguage getLanguage() const {
578 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
580 StringRef getProducer() const { return get()->getProducer(); }
581 bool isOptimized() const { return get()->isOptimized(); }
582 StringRef getFlags() const { return get()->getFlags(); }
583 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
585 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
586 DIArray getRetainedTypes() const {
587 return DIArray(get()->getRetainedTypes());
589 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
590 DIArray getGlobalVariables() const {
591 return DIArray(get()->getGlobalVariables());
593 DIArray getImportedEntities() const {
594 return DIArray(get()->getImportedEntities());
597 void replaceSubprograms(DIArray Subprograms);
598 void replaceGlobalVariables(DIArray GlobalVariables);
600 StringRef getSplitDebugFilename() const {
601 return get()->getSplitDebugFilename();
603 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
606 /// \brief This is a wrapper for a subprogram (e.g. a function).
607 class DISubprogram : public DIScope {
609 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
610 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
612 MDSubprogram *get() const {
613 return cast_or_null<MDSubprogram>(DIDescriptor::get());
615 operator MDSubprogram *() const { return get(); }
616 MDSubprogram *operator->() const { return get(); }
617 MDSubprogram &operator*() const {
618 assert(get() && "Expected valid pointer");
622 StringRef getName() const { return get()->getName(); }
623 StringRef getDisplayName() const { return get()->getDisplayName(); }
624 StringRef getLinkageName() const { return get()->getLinkageName(); }
625 unsigned getLineNumber() const { return get()->getLine(); }
627 /// \brief Check if this is local (like 'static' in C).
628 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
629 unsigned isDefinition() const { return get()->isDefinition(); }
631 unsigned getVirtuality() const { return get()->getVirtuality(); }
632 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
634 unsigned getFlags() const { return get()->getFlags(); }
636 unsigned isOptimized() const { return get()->isOptimized(); }
638 /// \brief Get the beginning of the scope of the function (not the name).
639 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
641 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
642 DISubroutineType getType() const {
643 return DISubroutineType(get()->getType());
646 DITypeRef getContainingType() const {
647 return DITypeRef::get(get()->getContainingType());
650 /// \brief Check if this provides debugging information for the function F.
651 bool describes(const Function *F);
653 Function *getFunction() const;
655 void replaceFunction(Function *F) {
657 N->replaceFunction(F);
659 DIArray getTemplateParams() const {
660 return DIArray(get()->getTemplateParams());
662 DISubprogram getFunctionDeclaration() const {
663 return DISubprogram(get()->getDeclaration());
665 MDNode *getVariablesNodes() const { return getVariables(); }
666 DIArray getVariables() const { return DIArray(get()->getVariables()); }
668 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
669 /// \brief Check for the "private" access specifier.
670 bool isPrivate() const {
671 return (getFlags() & FlagAccessibility) == FlagPrivate;
673 /// \brief Check for the "protected" access specifier.
674 bool isProtected() const {
675 return (getFlags() & FlagAccessibility) == FlagProtected;
677 /// \brief Check for the "public" access specifier.
678 bool isPublic() const {
679 return (getFlags() & FlagAccessibility) == FlagPublic;
681 /// \brief Check for "explicit".
682 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
683 /// \brief Check if this is prototyped.
684 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
686 /// \brief Check if this is reference-qualified.
688 /// Return true if this subprogram is a C++11 reference-qualified non-static
689 /// member function (void foo() &).
690 unsigned isLValueReference() const {
691 return (getFlags() & FlagLValueReference) != 0;
694 /// \brief Check if this is rvalue-reference-qualified.
696 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
697 /// non-static member function (void foo() &&).
698 unsigned isRValueReference() const {
699 return (getFlags() & FlagRValueReference) != 0;
703 /// \brief This is a wrapper for a lexical block.
704 class DILexicalBlock : public DIScope {
706 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
707 DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
709 MDLexicalBlockBase *get() const {
710 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
712 operator MDLexicalBlockBase *() const { return get(); }
713 MDLexicalBlockBase *operator->() const { return get(); }
714 MDLexicalBlockBase &operator*() const {
715 assert(get() && "Expected valid pointer");
719 DIScope getContext() const { return DIScope(get()->getScope()); }
720 unsigned getLineNumber() const {
721 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
725 unsigned getColumnNumber() const {
726 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
727 return N->getColumn();
732 /// \brief This is a wrapper for a lexical block with a filename change.
733 class DILexicalBlockFile : public DIScope {
735 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
736 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
738 MDLexicalBlockFile *get() const {
739 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
741 operator MDLexicalBlockFile *() const { return get(); }
742 MDLexicalBlockFile *operator->() const { return get(); }
743 MDLexicalBlockFile &operator*() const {
744 assert(get() && "Expected valid pointer");
748 DIScope getContext() const { return get()->getScope(); }
749 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
752 /// \brief A wrapper for a C++ style name space.
753 class DINameSpace : public DIScope {
755 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
756 DINameSpace(const MDNamespace *N) : DIScope(N) {}
758 MDNamespace *get() const {
759 return cast_or_null<MDNamespace>(DIDescriptor::get());
761 operator MDNamespace *() const { return get(); }
762 MDNamespace *operator->() const { return get(); }
763 MDNamespace &operator*() const {
764 assert(get() && "Expected valid pointer");
768 StringRef getName() const { return get()->getName(); }
769 unsigned getLineNumber() const { return get()->getLine(); }
770 DIScope getContext() const { return DIScope(get()->getScope()); }
773 /// \brief This is a wrapper for template type parameter.
774 class DITemplateTypeParameter : public DIDescriptor {
776 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
778 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
780 MDTemplateTypeParameter *get() const {
781 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
783 operator MDTemplateTypeParameter *() const { return get(); }
784 MDTemplateTypeParameter *operator->() const { return get(); }
785 MDTemplateTypeParameter &operator*() const {
786 assert(get() && "Expected valid pointer");
790 StringRef getName() const { return get()->getName(); }
792 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
795 /// \brief This is a wrapper for template value parameter.
796 class DITemplateValueParameter : public DIDescriptor {
798 explicit DITemplateValueParameter(const MDNode *N = nullptr)
800 DITemplateValueParameter(const MDTemplateValueParameter *N)
803 MDTemplateValueParameter *get() const {
804 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
806 operator MDTemplateValueParameter *() const { return get(); }
807 MDTemplateValueParameter *operator->() const { return get(); }
808 MDTemplateValueParameter &operator*() const {
809 assert(get() && "Expected valid pointer");
813 StringRef getName() const { return get()->getName(); }
814 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
815 Metadata *getValue() const { return get()->getValue(); }
818 /// \brief This is a wrapper for a global variable.
819 class DIGlobalVariable : public DIDescriptor {
820 DIFile getFile() const { return DIFile(get()->getFile()); }
823 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
824 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
826 MDGlobalVariable *get() const {
827 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
829 operator MDGlobalVariable *() const { return get(); }
830 MDGlobalVariable *operator->() const { return get(); }
831 MDGlobalVariable &operator*() const {
832 assert(get() && "Expected valid pointer");
836 StringRef getName() const { return get()->getName(); }
837 StringRef getDisplayName() const { return get()->getDisplayName(); }
838 StringRef getLinkageName() const { return get()->getLinkageName(); }
839 unsigned getLineNumber() const { return get()->getLine(); }
840 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
841 unsigned isDefinition() const { return get()->isDefinition(); }
843 DIScope getContext() const { return DIScope(get()->getScope()); }
844 StringRef getFilename() const { return getFile().getFilename(); }
845 StringRef getDirectory() const { return getFile().getDirectory(); }
846 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
848 GlobalVariable *getGlobal() const;
849 Constant *getConstant() const {
851 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
852 return C->getValue();
855 DIDerivedType getStaticDataMemberDeclaration() const {
856 return DIDerivedType(get()->getStaticDataMemberDeclaration());
860 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
861 class DIVariable : public DIDescriptor {
862 unsigned getFlags() const { return get()->getFlags(); }
865 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
866 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
868 MDLocalVariable *get() const {
869 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
871 operator MDLocalVariable *() const { return get(); }
872 MDLocalVariable *operator->() const { return get(); }
873 MDLocalVariable &operator*() const {
874 assert(get() && "Expected valid pointer");
878 StringRef getName() const { return get()->getName(); }
879 unsigned getLineNumber() const { return get()->getLine(); }
880 unsigned getArgNumber() const { return get()->getArg(); }
882 DIScope getContext() const { return DIScope(get()->getScope()); }
883 DIFile getFile() const { return DIFile(get()->getFile()); }
884 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
886 /// \brief Return true if this variable is marked as "artificial".
887 bool isArtificial() const {
888 return (getFlags() & FlagArtificial) != 0;
891 bool isObjectPointer() const {
892 return (getFlags() & FlagObjectPointer) != 0;
895 /// \brief If this variable is inlined then return inline location.
896 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
898 /// \brief Check if this is a "__block" variable (Apple Blocks).
899 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
900 return (getType().resolve(Map)).isBlockByrefStruct();
903 /// \brief Check if this is an inlined function argument.
904 bool isInlinedFnArgument(const Function *CurFn);
906 /// \brief Return the size reported by the variable's type.
907 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
909 void printExtendedName(raw_ostream &OS) const;
912 /// \brief A complex location expression in postfix notation.
914 /// This is (almost) a DWARF expression that modifies the location of a
915 /// variable or (or the location of a single piece of a variable).
917 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
918 /// and have DW_OP_plus consume the topmost elements on the stack.
919 class DIExpression : public DIDescriptor {
921 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
922 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
924 MDExpression *get() const {
925 return cast_or_null<MDExpression>(DIDescriptor::get());
927 operator MDExpression *() const { return get(); }
928 MDExpression *operator->() const { return get(); }
929 MDExpression &operator*() const {
930 assert(get() && "Expected valid pointer");
934 /// \brief Return the number of elements in the complex expression.
935 unsigned getNumElements() const { return get()->getNumElements(); }
937 /// \brief return the Idx'th complex address element.
938 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
940 /// \brief Return whether this is a piece of an aggregate variable.
941 bool isBitPiece() const;
942 /// \brief Return the offset of this piece in bits.
943 uint64_t getBitPieceOffset() const;
944 /// \brief Return the size of this piece in bits.
945 uint64_t getBitPieceSize() const;
948 /// \brief A lightweight wrapper around an element of a DIExpression.
950 friend class iterator;
951 MDExpression::element_iterator I;
953 Operand(MDExpression::element_iterator I) : I(I) {}
955 /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
956 /// Argument 0 is the operand itself.
957 uint64_t getArg(unsigned N) const {
958 MDExpression::element_iterator In = I;
962 operator uint64_t () const { return *I; }
963 /// \brief Returns underlying MDExpression::element_iterator.
964 const MDExpression::element_iterator &getBase() const { return I; }
965 /// \brief Returns the next operand.
966 iterator getNext() const;
969 /// \brief An iterator for DIExpression elements.
970 class iterator : public std::iterator<std::input_iterator_tag, StringRef,
971 unsigned, const Operand*, Operand> {
972 friend class Operand;
973 MDExpression::element_iterator I;
977 iterator(MDExpression::element_iterator I) : I(I) {}
978 const Operand &operator*() { return Tmp = Operand(I); }
979 const Operand *operator->() { return &(Tmp = Operand(I)); }
980 iterator &operator++() {
984 iterator operator++(int) {
989 bool operator==(const iterator &X) const { return I == X.I; }
990 bool operator!=(const iterator &X) const { return !(*this == X); }
995 case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
996 case dwarf::DW_OP_plus: std::advance(I, 2); break;
997 case dwarf::DW_OP_deref: std::advance(I, 1); break;
999 llvm_unreachable("unsupported operand");
1004 iterator begin() const { return get()->elements_begin(); }
1005 iterator end() const { return get()->elements_end(); }
1008 /// \brief This object holds location information.
1010 /// This object is not associated with any DWARF tag.
1011 class DILocation : public DIDescriptor {
1013 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
1014 DILocation(const MDLocation *N) : DIDescriptor(N) {}
1016 MDLocation *get() const {
1017 return cast_or_null<MDLocation>(DIDescriptor::get());
1019 operator MDLocation *() const { return get(); }
1020 MDLocation *operator->() const { return get(); }
1021 MDLocation &operator*() const {
1022 assert(get() && "Expected valid pointer");
1026 unsigned getLineNumber() const { return get()->getLine(); }
1027 unsigned getColumnNumber() const { return get()->getColumn(); }
1028 DIScope getScope() const { return DIScope(get()->getScope()); }
1029 DILocation getOrigLocation() const {
1030 return DILocation(get()->getInlinedAt());
1032 StringRef getFilename() const { return getScope().getFilename(); }
1033 StringRef getDirectory() const { return getScope().getDirectory(); }
1034 bool atSameLineAs(const DILocation &Other) const {
1035 return (getLineNumber() == Other.getLineNumber() &&
1036 getFilename() == Other.getFilename());
1038 /// \brief Get the DWAF discriminator.
1040 /// DWARF discriminators are used to distinguish identical file locations for
1041 /// instructions that are on different basic blocks. If two instructions are
1042 /// inside the same lexical block and are in different basic blocks, we
1043 /// create a new lexical block with identical location as the original but
1044 /// with a different discriminator value
1045 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1046 unsigned getDiscriminator() const {
1047 // Since discriminators are associated with lexical blocks, make
1048 // sure this location is a lexical block before retrieving its
1050 if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
1051 return F->getDiscriminator();
1055 /// \brief Generate a new discriminator value for this location.
1056 unsigned computeNewDiscriminator(LLVMContext &Ctx);
1058 /// \brief Return a copy of this location with a different scope.
1059 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1062 class DIObjCProperty : public DIDescriptor {
1064 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
1065 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1067 MDObjCProperty *get() const {
1068 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1070 operator MDObjCProperty *() const { return get(); }
1071 MDObjCProperty *operator->() const { return get(); }
1072 MDObjCProperty &operator*() const {
1073 assert(get() && "Expected valid pointer");
1077 StringRef getObjCPropertyName() const { return get()->getName(); }
1078 DIFile getFile() const { return DIFile(get()->getFile()); }
1079 unsigned getLineNumber() const { return get()->getLine(); }
1081 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1082 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1083 unsigned getAttributes() const { return get()->getAttributes(); }
1084 bool isReadOnlyObjCProperty() const {
1085 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1087 bool isReadWriteObjCProperty() const {
1088 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1090 bool isAssignObjCProperty() const {
1091 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1093 bool isRetainObjCProperty() const {
1094 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1096 bool isCopyObjCProperty() const {
1097 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1099 bool isNonAtomicObjCProperty() const {
1100 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1103 /// \brief Get the type.
1105 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1106 /// the type as a DITypeRef here.
1107 DIType getType() const { return DIType(get()->getType()); }
1110 /// \brief An imported module (C++ using directive or similar).
1111 class DIImportedEntity : public DIDescriptor {
1113 DIImportedEntity() = default;
1114 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
1115 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1117 MDImportedEntity *get() const {
1118 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1120 operator MDImportedEntity *() const { return get(); }
1121 MDImportedEntity *operator->() const { return get(); }
1122 MDImportedEntity &operator*() const {
1123 assert(get() && "Expected valid pointer");
1127 DIScope getContext() const { return DIScope(get()->getScope()); }
1128 DIDescriptorRef getEntity() const {
1129 return DIDescriptorRef::get(get()->getEntity());
1131 unsigned getLineNumber() const { return get()->getLine(); }
1132 StringRef getName() const { return get()->getName(); }
1135 /// \brief Find subprogram that is enclosing this scope.
1136 DISubprogram getDISubprogram(const MDNode *Scope);
1138 /// \brief Find debug info for a given function.
1139 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1141 DISubprogram getDISubprogram(const Function *F);
1143 /// \brief Find underlying composite type.
1144 DICompositeType getDICompositeType(DIType T);
1146 /// \brief Create a new inlined variable based on current variable.
1148 /// @param DV Current Variable.
1149 /// @param InlinedScope Location at current variable is inlined.
1150 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1151 LLVMContext &VMContext);
1153 /// \brief Remove inlined scope from the variable.
1154 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1156 /// \brief Generate map by visiting all retained types.
1157 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1159 /// \brief Strip debug info in the module if it exists.
1161 /// To do this, we remove all calls to the debugger intrinsics and any named
1162 /// metadata for debugging. We also remove debug locations for instructions.
1163 /// Return true if module is modified.
1164 bool StripDebugInfo(Module &M);
1165 bool stripDebugInfo(Function &F);
1167 /// \brief Return Debug Info Metadata Version by checking module flags.
1168 unsigned getDebugMetadataVersionFromModule(const Module &M);
1170 /// \brief Utility to find all debug info in a module.
1172 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1173 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1174 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1175 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1176 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1177 /// used by the CUs.
1178 class DebugInfoFinder {
1180 DebugInfoFinder() : TypeMapInitialized(false) {}
1182 /// \brief Process entire module and collect debug info anchors.
1183 void processModule(const Module &M);
1185 /// \brief Process DbgDeclareInst.
1186 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1187 /// \brief Process DbgValueInst.
1188 void processValue(const Module &M, const DbgValueInst *DVI);
1189 /// \brief Process DILocation.
1190 void processLocation(const Module &M, DILocation Loc);
1192 /// \brief Clear all lists.
1196 void InitializeTypeMap(const Module &M);
1198 void processType(DIType DT);
1199 void processSubprogram(DISubprogram SP);
1200 void processScope(DIScope Scope);
1201 bool addCompileUnit(DICompileUnit CU);
1202 bool addGlobalVariable(DIGlobalVariable DIG);
1203 bool addSubprogram(DISubprogram SP);
1204 bool addType(DIType DT);
1205 bool addScope(DIScope Scope);
1208 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1209 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1210 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1211 global_variable_iterator;
1212 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1213 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1215 iterator_range<compile_unit_iterator> compile_units() const {
1216 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1219 iterator_range<subprogram_iterator> subprograms() const {
1220 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1223 iterator_range<global_variable_iterator> global_variables() const {
1224 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1227 iterator_range<type_iterator> types() const {
1228 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1231 iterator_range<scope_iterator> scopes() const {
1232 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1235 unsigned compile_unit_count() const { return CUs.size(); }
1236 unsigned global_variable_count() const { return GVs.size(); }
1237 unsigned subprogram_count() const { return SPs.size(); }
1238 unsigned type_count() const { return TYs.size(); }
1239 unsigned scope_count() const { return Scopes.size(); }
1242 SmallVector<DICompileUnit, 8> CUs;
1243 SmallVector<DISubprogram, 8> SPs;
1244 SmallVector<DIGlobalVariable, 8> GVs;
1245 SmallVector<DIType, 8> TYs;
1246 SmallVector<DIScope, 8> Scopes;
1247 SmallPtrSet<MDNode *, 64> NodesSeen;
1248 DITypeIdentifierMap TypeIdentifierMap;
1250 /// \brief Specify if TypeIdentifierMap is initialized.
1251 bool TypeMapInitialized;
1254 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1256 } // end namespace llvm