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); }
305 DIDescriptor DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const;
307 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const;
308 template <> DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const;
310 /// \brief Handle fields that are references to DIDescriptors.
312 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const;
313 /// \brief Specialize DIRef constructor for DIDescriptorRef.
314 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V);
316 /// \brief Handle fields that are references to DIScopes.
317 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
318 /// \brief Specialize DIRef constructor for DIScopeRef.
319 template <> DIRef<DIScope>::DIRef(const Metadata *V);
321 /// \brief Handle fields that are references to DITypes.
322 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
323 /// \brief Specialize DIRef constructor for DITypeRef.
324 template <> DIRef<DIType>::DIRef(const Metadata *V);
326 /// \brief This is a wrapper for a type.
328 /// FIXME: Types should be factored much better so that CV qualifiers and
329 /// others do not require a huge and empty descriptor full of zeros.
330 class DIType : public DIScope {
332 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
333 DIType(const MDType *N) : DIScope(N) {}
335 MDType *get() const { return cast_or_null<MDType>(DIDescriptor::get()); }
336 operator MDType *() const { return get(); }
337 MDType *operator->() const { return get(); }
338 MDType &operator*() const {
339 assert(get() && "Expected valid pointer");
343 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
344 StringRef getName() const { return get()->getName(); }
345 unsigned getLineNumber() const { return get()->getLine(); }
346 uint64_t getSizeInBits() const { return get()->getSizeInBits(); }
347 uint64_t getAlignInBits() const { return get()->getAlignInBits(); }
348 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
349 // carry this is just plain insane.
350 uint64_t getOffsetInBits() const { return get()->getOffsetInBits(); }
351 unsigned getFlags() const { return get()->getFlags(); }
352 bool isPrivate() const {
353 return (getFlags() & FlagAccessibility) == FlagPrivate;
355 bool isProtected() const {
356 return (getFlags() & FlagAccessibility) == FlagProtected;
358 bool isPublic() const {
359 return (getFlags() & FlagAccessibility) == FlagPublic;
361 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
362 bool isAppleBlockExtension() const {
363 return (getFlags() & FlagAppleBlock) != 0;
365 bool isBlockByrefStruct() const {
366 return (getFlags() & FlagBlockByrefStruct) != 0;
368 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
369 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
370 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
371 bool isObjcClassComplete() const {
372 return (getFlags() & FlagObjcClassComplete) != 0;
374 bool isVector() const { return (getFlags() & FlagVector) != 0; }
375 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
376 bool isLValueReference() const {
377 return (getFlags() & FlagLValueReference) != 0;
379 bool isRValueReference() const {
380 return (getFlags() & FlagRValueReference) != 0;
382 bool isValid() const { return DbgNode && isType(); }
385 /// \brief A basic type, like 'int' or 'float'.
386 class DIBasicType : public DIType {
388 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
389 DIBasicType(const MDBasicType *N) : DIType(N) {}
391 MDBasicType *get() const {
392 return cast_or_null<MDBasicType>(DIDescriptor::get());
394 operator MDBasicType *() const { return get(); }
395 MDBasicType *operator->() const { return get(); }
396 MDBasicType &operator*() const {
397 assert(get() && "Expected valid pointer");
401 unsigned getEncoding() const { return get()->getEncoding(); }
404 /// \brief A simple derived type
406 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
407 /// Or, a data member of a class/struct/union.
408 class DIDerivedType : public DIType {
410 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
411 DIDerivedType(const MDDerivedTypeBase *N) : DIType(N) {}
413 MDDerivedTypeBase *get() const {
414 return cast_or_null<MDDerivedTypeBase>(DIDescriptor::get());
416 operator MDDerivedTypeBase *() const { return get(); }
417 MDDerivedTypeBase *operator->() const { return get(); }
418 MDDerivedTypeBase &operator*() const {
419 assert(get() && "Expected valid pointer");
423 DITypeRef getTypeDerivedFrom() const {
424 return DITypeRef::get(get()->getBaseType());
427 /// \brief Return property node, if this ivar is associated with one.
428 MDNode *getObjCProperty() const {
429 if (auto *N = dyn_cast<MDDerivedType>(get()))
430 return dyn_cast_or_null<MDNode>(N->getExtraData());
434 DITypeRef getClassType() const {
435 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
436 if (auto *N = dyn_cast<MDDerivedType>(get()))
437 return DITypeRef::get(N->getExtraData());
438 return DITypeRef::get(nullptr);
441 Constant *getConstant() const {
442 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
443 if (auto *N = dyn_cast<MDDerivedType>(get()))
444 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getExtraData()))
445 return C->getValue();
451 /// \brief Types that refer to multiple other types.
453 /// This descriptor holds a type that can refer to multiple other types, like a
454 /// function or struct.
456 /// DICompositeType is derived from DIDerivedType because some
457 /// composite types (such as enums) can be derived from basic types
458 // FIXME: Make this derive from DIType directly & just store the
459 // base type in a single DIType field.
460 class DICompositeType : public DIDerivedType {
461 friend class DIBuilder;
463 /// \brief Set the array of member DITypes.
464 void setArraysHelper(MDNode *Elements, MDNode *TParams);
467 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
468 DICompositeType(const MDCompositeTypeBase *N) : DIDerivedType(N) {}
470 MDCompositeTypeBase *get() const {
471 return cast_or_null<MDCompositeTypeBase>(DIDescriptor::get());
473 operator MDCompositeTypeBase *() const { return get(); }
474 MDCompositeTypeBase *operator->() const { return get(); }
475 MDCompositeTypeBase &operator*() const {
476 assert(get() && "Expected valid pointer");
480 DIArray getElements() const {
481 assert(!isSubroutineType() && "no elements for DISubroutineType");
482 return DIArray(get()->getElements());
486 template <typename T>
487 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
489 (!TParams || DbgNode->getNumOperands() == 8) &&
490 "If you're setting the template parameters this should include a slot "
492 setArraysHelper(Elements, TParams);
496 unsigned getRunTimeLang() const { return get()->getRuntimeLang(); }
497 DITypeRef getContainingType() const {
498 return DITypeRef::get(get()->getVTableHolder());
502 /// \brief Set the containing type.
503 void setContainingType(DICompositeType ContainingType);
506 DIArray getTemplateParams() const {
507 return DIArray(get()->getTemplateParams());
509 MDString *getIdentifier() const { return get()->getRawIdentifier(); }
512 class DISubroutineType : public DICompositeType {
514 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
515 DISubroutineType(const MDSubroutineType *N) : DICompositeType(N) {}
517 MDSubroutineType *get() const {
518 return cast_or_null<MDSubroutineType>(DIDescriptor::get());
520 operator MDSubroutineType *() const { return get(); }
521 MDSubroutineType *operator->() const { return get(); }
522 MDSubroutineType &operator*() const {
523 assert(get() && "Expected valid pointer");
527 DITypedArray<DITypeRef> getTypeArray() const {
528 return DITypedArray<DITypeRef>(get()->getTypeArray());
532 /// \brief This is a wrapper for a file.
533 class DIFile : public DIScope {
535 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
536 DIFile(const MDFile *N) : DIScope(N) {}
538 MDFile *get() const { return cast_or_null<MDFile>(DIDescriptor::get()); }
539 operator MDFile *() const { return get(); }
540 MDFile *operator->() const { return get(); }
541 MDFile &operator*() const {
542 assert(get() && "Expected valid pointer");
546 /// \brief Retrieve the MDNode for the directory/file pair.
547 MDNode *getFileNode() const { return get(); }
550 /// \brief A wrapper for a compile unit.
551 class DICompileUnit : public DIScope {
553 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
554 DICompileUnit(const MDCompileUnit *N) : DIScope(N) {}
556 MDCompileUnit *get() const {
557 return cast_or_null<MDCompileUnit>(DIDescriptor::get());
559 operator MDCompileUnit *() const { return get(); }
560 MDCompileUnit *operator->() const { return get(); }
561 MDCompileUnit &operator*() const {
562 assert(get() && "Expected valid pointer");
566 dwarf::SourceLanguage getLanguage() const {
567 return static_cast<dwarf::SourceLanguage>(get()->getSourceLanguage());
569 StringRef getProducer() const { return get()->getProducer(); }
570 bool isOptimized() const { return get()->isOptimized(); }
571 StringRef getFlags() const { return get()->getFlags(); }
572 unsigned getRunTimeVersion() const { return get()->getRuntimeVersion(); }
574 DIArray getEnumTypes() const { return DIArray(get()->getEnumTypes()); }
575 DIArray getRetainedTypes() const {
576 return DIArray(get()->getRetainedTypes());
578 DIArray getSubprograms() const { return DIArray(get()->getSubprograms()); }
579 DIArray getGlobalVariables() const {
580 return DIArray(get()->getGlobalVariables());
582 DIArray getImportedEntities() const {
583 return DIArray(get()->getImportedEntities());
586 void replaceSubprograms(DIArray Subprograms);
587 void replaceGlobalVariables(DIArray GlobalVariables);
589 StringRef getSplitDebugFilename() const {
590 return get()->getSplitDebugFilename();
592 unsigned getEmissionKind() const { return get()->getEmissionKind(); }
595 /// \brief This is a wrapper for a subprogram (e.g. a function).
596 class DISubprogram : public DIScope {
598 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
599 DISubprogram(const MDSubprogram *N) : DIScope(N) {}
601 MDSubprogram *get() const {
602 return cast_or_null<MDSubprogram>(DIDescriptor::get());
604 operator MDSubprogram *() const { return get(); }
605 MDSubprogram *operator->() const { return get(); }
606 MDSubprogram &operator*() const {
607 assert(get() && "Expected valid pointer");
611 StringRef getName() const { return get()->getName(); }
612 StringRef getDisplayName() const { return get()->getDisplayName(); }
613 StringRef getLinkageName() const { return get()->getLinkageName(); }
614 unsigned getLineNumber() const { return get()->getLine(); }
616 /// \brief Check if this is local (like 'static' in C).
617 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
618 unsigned isDefinition() const { return get()->isDefinition(); }
620 unsigned getVirtuality() const { return get()->getVirtuality(); }
621 unsigned getVirtualIndex() const { return get()->getVirtualIndex(); }
623 unsigned getFlags() const { return get()->getFlags(); }
625 unsigned isOptimized() const { return get()->isOptimized(); }
627 /// \brief Get the beginning of the scope of the function (not the name).
628 unsigned getScopeLineNumber() const { return get()->getScopeLine(); }
630 DIScopeRef getContext() const { return DIScopeRef::get(get()->getScope()); }
631 DISubroutineType getType() const {
632 return DISubroutineType(get()->getType());
635 DITypeRef getContainingType() const {
636 return DITypeRef::get(get()->getContainingType());
639 /// \brief Check if this provides debugging information for the function F.
640 bool describes(const Function *F);
642 Function *getFunction() const;
644 void replaceFunction(Function *F) {
646 N->replaceFunction(F);
648 DIArray getTemplateParams() const {
649 return DIArray(get()->getTemplateParams());
651 DISubprogram getFunctionDeclaration() const {
652 return DISubprogram(get()->getDeclaration());
654 MDNode *getVariablesNodes() const { return getVariables(); }
655 DIArray getVariables() const { return DIArray(get()->getVariables()); }
657 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
658 /// \brief Check for the "private" access specifier.
659 bool isPrivate() const {
660 return (getFlags() & FlagAccessibility) == FlagPrivate;
662 /// \brief Check for the "protected" access specifier.
663 bool isProtected() const {
664 return (getFlags() & FlagAccessibility) == FlagProtected;
666 /// \brief Check for the "public" access specifier.
667 bool isPublic() const {
668 return (getFlags() & FlagAccessibility) == FlagPublic;
670 /// \brief Check for "explicit".
671 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
672 /// \brief Check if this is prototyped.
673 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
675 /// \brief Check if this is reference-qualified.
677 /// Return true if this subprogram is a C++11 reference-qualified non-static
678 /// member function (void foo() &).
679 unsigned isLValueReference() const {
680 return (getFlags() & FlagLValueReference) != 0;
683 /// \brief Check if this is rvalue-reference-qualified.
685 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
686 /// non-static member function (void foo() &&).
687 unsigned isRValueReference() const {
688 return (getFlags() & FlagRValueReference) != 0;
692 /// \brief This is a wrapper for a lexical block.
693 class DILexicalBlock : public DIScope {
695 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
696 DILexicalBlock(const MDLexicalBlockBase *N) : DIScope(N) {}
698 MDLexicalBlockBase *get() const {
699 return cast_or_null<MDLexicalBlockBase>(DIDescriptor::get());
701 operator MDLexicalBlockBase *() const { return get(); }
702 MDLexicalBlockBase *operator->() const { return get(); }
703 MDLexicalBlockBase &operator*() const {
704 assert(get() && "Expected valid pointer");
708 DIScope getContext() const { return DIScope(get()->getScope()); }
709 unsigned getLineNumber() const {
710 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
714 unsigned getColumnNumber() const {
715 if (auto *N = dyn_cast<MDLexicalBlock>(get()))
716 return N->getColumn();
721 /// \brief This is a wrapper for a lexical block with a filename change.
722 class DILexicalBlockFile : public DIScope {
724 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
725 DILexicalBlockFile(const MDLexicalBlockFile *N) : DIScope(N) {}
727 MDLexicalBlockFile *get() const {
728 return cast_or_null<MDLexicalBlockFile>(DIDescriptor::get());
730 operator MDLexicalBlockFile *() const { return get(); }
731 MDLexicalBlockFile *operator->() const { return get(); }
732 MDLexicalBlockFile &operator*() const {
733 assert(get() && "Expected valid pointer");
737 DIScope getContext() const { return get()->getScope(); }
738 unsigned getDiscriminator() const { return get()->getDiscriminator(); }
741 /// \brief A wrapper for a C++ style name space.
742 class DINameSpace : public DIScope {
744 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
745 DINameSpace(const MDNamespace *N) : DIScope(N) {}
747 MDNamespace *get() const {
748 return cast_or_null<MDNamespace>(DIDescriptor::get());
750 operator MDNamespace *() const { return get(); }
751 MDNamespace *operator->() const { return get(); }
752 MDNamespace &operator*() const {
753 assert(get() && "Expected valid pointer");
757 StringRef getName() const { return get()->getName(); }
758 unsigned getLineNumber() const { return get()->getLine(); }
759 DIScope getContext() const { return DIScope(get()->getScope()); }
762 /// \brief This is a wrapper for template type parameter.
763 class DITemplateTypeParameter : public DIDescriptor {
765 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
767 DITemplateTypeParameter(const MDTemplateTypeParameter *N) : DIDescriptor(N) {}
769 MDTemplateTypeParameter *get() const {
770 return cast_or_null<MDTemplateTypeParameter>(DIDescriptor::get());
772 operator MDTemplateTypeParameter *() const { return get(); }
773 MDTemplateTypeParameter *operator->() const { return get(); }
774 MDTemplateTypeParameter &operator*() const {
775 assert(get() && "Expected valid pointer");
779 StringRef getName() const { return get()->getName(); }
781 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
784 /// \brief This is a wrapper for template value parameter.
785 class DITemplateValueParameter : public DIDescriptor {
787 explicit DITemplateValueParameter(const MDNode *N = nullptr)
789 DITemplateValueParameter(const MDTemplateValueParameter *N)
792 MDTemplateValueParameter *get() const {
793 return cast_or_null<MDTemplateValueParameter>(DIDescriptor::get());
795 operator MDTemplateValueParameter *() const { return get(); }
796 MDTemplateValueParameter *operator->() const { return get(); }
797 MDTemplateValueParameter &operator*() const {
798 assert(get() && "Expected valid pointer");
802 StringRef getName() const { return get()->getName(); }
803 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
804 Metadata *getValue() const { return get()->getValue(); }
807 /// \brief This is a wrapper for a global variable.
808 class DIGlobalVariable : public DIDescriptor {
809 DIFile getFile() const { return DIFile(get()->getFile()); }
812 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
813 DIGlobalVariable(const MDGlobalVariable *N) : DIDescriptor(N) {}
815 MDGlobalVariable *get() const {
816 return cast_or_null<MDGlobalVariable>(DIDescriptor::get());
818 operator MDGlobalVariable *() const { return get(); }
819 MDGlobalVariable *operator->() const { return get(); }
820 MDGlobalVariable &operator*() const {
821 assert(get() && "Expected valid pointer");
825 StringRef getName() const { return get()->getName(); }
826 StringRef getDisplayName() const { return get()->getDisplayName(); }
827 StringRef getLinkageName() const { return get()->getLinkageName(); }
828 unsigned getLineNumber() const { return get()->getLine(); }
829 unsigned isLocalToUnit() const { return get()->isLocalToUnit(); }
830 unsigned isDefinition() const { return get()->isDefinition(); }
832 DIScope getContext() const { return DIScope(get()->getScope()); }
833 StringRef getFilename() const { return getFile().getFilename(); }
834 StringRef getDirectory() const { return getFile().getDirectory(); }
835 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
837 GlobalVariable *getGlobal() const;
838 Constant *getConstant() const {
840 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getVariable()))
841 return C->getValue();
844 DIDerivedType getStaticDataMemberDeclaration() const {
845 return DIDerivedType(get()->getStaticDataMemberDeclaration());
849 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
850 class DIVariable : public DIDescriptor {
851 unsigned getFlags() const { return get()->getFlags(); }
854 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
855 DIVariable(const MDLocalVariable *N) : DIDescriptor(N) {}
857 MDLocalVariable *get() const {
858 return cast_or_null<MDLocalVariable>(DIDescriptor::get());
860 operator MDLocalVariable *() const { return get(); }
861 MDLocalVariable *operator->() const { return get(); }
862 MDLocalVariable &operator*() const {
863 assert(get() && "Expected valid pointer");
867 StringRef getName() const { return get()->getName(); }
868 unsigned getLineNumber() const { return get()->getLine(); }
869 unsigned getArgNumber() const { return get()->getArg(); }
871 DIScope getContext() const { return DIScope(get()->getScope()); }
872 DIFile getFile() const { return DIFile(get()->getFile()); }
873 DITypeRef getType() const { return DITypeRef::get(get()->getType()); }
875 /// \brief Return true if this variable is marked as "artificial".
876 bool isArtificial() const {
877 return (getFlags() & FlagArtificial) != 0;
880 bool isObjectPointer() const {
881 return (getFlags() & FlagObjectPointer) != 0;
884 /// \brief If this variable is inlined then return inline location.
885 MDNode *getInlinedAt() const { return DIDescriptor(get()->getInlinedAt()); }
887 /// \brief Check if this is a "__block" variable (Apple Blocks).
888 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
889 return (getType().resolve(Map)).isBlockByrefStruct();
892 /// \brief Check if this is an inlined function argument.
893 bool isInlinedFnArgument(const Function *CurFn);
895 /// \brief Return the size reported by the variable's type.
896 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
898 void printExtendedName(raw_ostream &OS) const;
901 /// \brief A complex location expression in postfix notation.
903 /// This is (almost) a DWARF expression that modifies the location of a
904 /// variable or (or the location of a single piece of a variable).
906 /// FIXME: Instead of DW_OP_plus taking an argument, this should use DW_OP_const
907 /// and have DW_OP_plus consume the topmost elements on the stack.
908 class DIExpression : public DIDescriptor {
910 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
911 DIExpression(const MDExpression *N) : DIDescriptor(N) {}
913 MDExpression *get() const {
914 return cast_or_null<MDExpression>(DIDescriptor::get());
916 operator MDExpression *() const { return get(); }
917 MDExpression *operator->() const { return get(); }
918 MDExpression &operator*() const {
919 assert(get() && "Expected valid pointer");
923 /// \brief Return the number of elements in the complex expression.
924 unsigned getNumElements() const { return get()->getNumElements(); }
926 /// \brief return the Idx'th complex address element.
927 uint64_t getElement(unsigned I) const { return get()->getElement(I); }
929 /// \brief Return whether this is a piece of an aggregate variable.
930 bool isBitPiece() const;
931 /// \brief Return the offset of this piece in bits.
932 uint64_t getBitPieceOffset() const;
933 /// \brief Return the size of this piece in bits.
934 uint64_t getBitPieceSize() const;
937 /// \brief A lightweight wrapper around an element of a DIExpression.
939 friend class iterator;
940 MDExpression::element_iterator I;
942 Operand(MDExpression::element_iterator I) : I(I) {}
944 /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
945 /// Argument 0 is the operand itself.
946 uint64_t getArg(unsigned N) const {
947 MDExpression::element_iterator In = I;
951 operator uint64_t () const { return *I; }
952 /// \brief Returns underlying MDExpression::element_iterator.
953 const MDExpression::element_iterator &getBase() const { return I; }
954 /// \brief Returns the next operand.
955 iterator getNext() const;
958 /// \brief An iterator for DIExpression elements.
959 class iterator : public std::iterator<std::input_iterator_tag, StringRef,
960 unsigned, const Operand*, Operand> {
961 friend class Operand;
962 MDExpression::element_iterator I;
966 iterator(MDExpression::element_iterator I) : I(I) {}
967 const Operand &operator*() { return Tmp = Operand(I); }
968 const Operand *operator->() { return &(Tmp = Operand(I)); }
969 iterator &operator++() {
973 iterator operator++(int) {
978 bool operator==(const iterator &X) const { return I == X.I; }
979 bool operator!=(const iterator &X) const { return !(*this == X); }
984 case dwarf::DW_OP_bit_piece: std::advance(I, 3); break;
985 case dwarf::DW_OP_plus: std::advance(I, 2); break;
986 case dwarf::DW_OP_deref: std::advance(I, 1); break;
988 llvm_unreachable("unsupported operand");
993 iterator begin() const { return get()->elements_begin(); }
994 iterator end() const { return get()->elements_end(); }
997 /// \brief This object holds location information.
999 /// This object is not associated with any DWARF tag.
1000 class DILocation : public DIDescriptor {
1002 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
1003 DILocation(const MDLocation *N) : DIDescriptor(N) {}
1005 MDLocation *get() const {
1006 return cast_or_null<MDLocation>(DIDescriptor::get());
1008 operator MDLocation *() const { return get(); }
1009 MDLocation *operator->() const { return get(); }
1010 MDLocation &operator*() const {
1011 assert(get() && "Expected valid pointer");
1015 unsigned getLineNumber() const { return get()->getLine(); }
1016 unsigned getColumnNumber() const { return get()->getColumn(); }
1017 DIScope getScope() const { return DIScope(get()->getScope()); }
1018 DILocation getOrigLocation() const {
1019 return DILocation(get()->getInlinedAt());
1021 StringRef getFilename() const { return getScope().getFilename(); }
1022 StringRef getDirectory() const { return getScope().getDirectory(); }
1023 bool atSameLineAs(const DILocation &Other) const {
1024 return (getLineNumber() == Other.getLineNumber() &&
1025 getFilename() == Other.getFilename());
1027 /// \brief Get the DWAF discriminator.
1029 /// DWARF discriminators are used to distinguish identical file locations for
1030 /// instructions that are on different basic blocks. If two instructions are
1031 /// inside the same lexical block and are in different basic blocks, we
1032 /// create a new lexical block with identical location as the original but
1033 /// with a different discriminator value
1034 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
1035 unsigned getDiscriminator() const {
1036 // Since discriminators are associated with lexical blocks, make
1037 // sure this location is a lexical block before retrieving its
1039 if (auto *F = dyn_cast<MDLexicalBlockFile>(get()->getScope()))
1040 return F->getDiscriminator();
1044 /// \brief Generate a new discriminator value for this location.
1045 unsigned computeNewDiscriminator(LLVMContext &Ctx);
1047 /// \brief Return a copy of this location with a different scope.
1048 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
1051 class DIObjCProperty : public DIDescriptor {
1053 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
1054 DIObjCProperty(const MDObjCProperty *N) : DIDescriptor(N) {}
1056 MDObjCProperty *get() const {
1057 return cast_or_null<MDObjCProperty>(DIDescriptor::get());
1059 operator MDObjCProperty *() const { return get(); }
1060 MDObjCProperty *operator->() const { return get(); }
1061 MDObjCProperty &operator*() const {
1062 assert(get() && "Expected valid pointer");
1066 StringRef getObjCPropertyName() const { return get()->getName(); }
1067 DIFile getFile() const { return DIFile(get()->getFile()); }
1068 unsigned getLineNumber() const { return get()->getLine(); }
1070 StringRef getObjCPropertyGetterName() const { return get()->getGetterName(); }
1071 StringRef getObjCPropertySetterName() const { return get()->getSetterName(); }
1072 unsigned getAttributes() const { return get()->getAttributes(); }
1073 bool isReadOnlyObjCProperty() const {
1074 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1076 bool isReadWriteObjCProperty() const {
1077 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1079 bool isAssignObjCProperty() const {
1080 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1082 bool isRetainObjCProperty() const {
1083 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1085 bool isCopyObjCProperty() const {
1086 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1088 bool isNonAtomicObjCProperty() const {
1089 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1092 /// \brief Get the type.
1094 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1095 /// the type as a DITypeRef here.
1096 DIType getType() const { return DIType(get()->getType()); }
1099 /// \brief An imported module (C++ using directive or similar).
1100 class DIImportedEntity : public DIDescriptor {
1102 DIImportedEntity() = default;
1103 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
1104 DIImportedEntity(const MDImportedEntity *N) : DIDescriptor(N) {}
1106 MDImportedEntity *get() const {
1107 return cast_or_null<MDImportedEntity>(DIDescriptor::get());
1109 operator MDImportedEntity *() const { return get(); }
1110 MDImportedEntity *operator->() const { return get(); }
1111 MDImportedEntity &operator*() const {
1112 assert(get() && "Expected valid pointer");
1116 DIScope getContext() const { return DIScope(get()->getScope()); }
1117 DIDescriptorRef getEntity() const {
1118 return DIDescriptorRef::get(get()->getEntity());
1120 unsigned getLineNumber() const { return get()->getLine(); }
1121 StringRef getName() const { return get()->getName(); }
1124 /// \brief Find subprogram that is enclosing this scope.
1125 DISubprogram getDISubprogram(const MDNode *Scope);
1127 /// \brief Find debug info for a given function.
1128 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1130 DISubprogram getDISubprogram(const Function *F);
1132 /// \brief Find underlying composite type.
1133 DICompositeType getDICompositeType(DIType T);
1135 /// \brief Create a new inlined variable based on current variable.
1137 /// @param DV Current Variable.
1138 /// @param InlinedScope Location at current variable is inlined.
1139 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1140 LLVMContext &VMContext);
1142 /// \brief Remove inlined scope from the variable.
1143 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1145 /// \brief Generate map by visiting all retained types.
1146 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1148 /// \brief Strip debug info in the module if it exists.
1150 /// To do this, we remove all calls to the debugger intrinsics and any named
1151 /// metadata for debugging. We also remove debug locations for instructions.
1152 /// Return true if module is modified.
1153 bool StripDebugInfo(Module &M);
1154 bool stripDebugInfo(Function &F);
1156 /// \brief Return Debug Info Metadata Version by checking module flags.
1157 unsigned getDebugMetadataVersionFromModule(const Module &M);
1159 /// \brief Utility to find all debug info in a module.
1161 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1162 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1163 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1164 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1165 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1166 /// used by the CUs.
1167 class DebugInfoFinder {
1169 DebugInfoFinder() : TypeMapInitialized(false) {}
1171 /// \brief Process entire module and collect debug info anchors.
1172 void processModule(const Module &M);
1174 /// \brief Process DbgDeclareInst.
1175 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1176 /// \brief Process DbgValueInst.
1177 void processValue(const Module &M, const DbgValueInst *DVI);
1178 /// \brief Process DILocation.
1179 void processLocation(const Module &M, DILocation Loc);
1181 /// \brief Clear all lists.
1185 void InitializeTypeMap(const Module &M);
1187 void processType(DIType DT);
1188 void processSubprogram(DISubprogram SP);
1189 void processScope(DIScope Scope);
1190 bool addCompileUnit(DICompileUnit CU);
1191 bool addGlobalVariable(DIGlobalVariable DIG);
1192 bool addSubprogram(DISubprogram SP);
1193 bool addType(DIType DT);
1194 bool addScope(DIScope Scope);
1197 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1198 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1199 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator
1200 global_variable_iterator;
1201 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1202 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1204 iterator_range<compile_unit_iterator> compile_units() const {
1205 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1208 iterator_range<subprogram_iterator> subprograms() const {
1209 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1212 iterator_range<global_variable_iterator> global_variables() const {
1213 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1216 iterator_range<type_iterator> types() const {
1217 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1220 iterator_range<scope_iterator> scopes() const {
1221 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1224 unsigned compile_unit_count() const { return CUs.size(); }
1225 unsigned global_variable_count() const { return GVs.size(); }
1226 unsigned subprogram_count() const { return SPs.size(); }
1227 unsigned type_count() const { return TYs.size(); }
1228 unsigned scope_count() const { return Scopes.size(); }
1231 SmallVector<DICompileUnit, 8> CUs;
1232 SmallVector<DISubprogram, 8> SPs;
1233 SmallVector<DIGlobalVariable, 8> GVs;
1234 SmallVector<DIType, 8> TYs;
1235 SmallVector<DIScope, 8> Scopes;
1236 SmallPtrSet<MDNode *, 64> NodesSeen;
1237 DITypeIdentifierMap TypeIdentifierMap;
1239 /// \brief Specify if TypeIdentifierMap is initialized.
1240 bool TypeMapInitialized;
1243 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1245 } // end namespace llvm