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/Metadata.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 class DIHeaderFieldIterator
62 : public std::iterator<std::forward_iterator_tag, StringRef, std::ptrdiff_t,
63 const StringRef *, StringRef> {
68 DIHeaderFieldIterator() {}
69 DIHeaderFieldIterator(StringRef Header)
70 : Header(Header), Current(Header.slice(0, Header.find('\0'))) {}
71 StringRef operator*() const { return Current; }
72 const StringRef * operator->() const { return &Current; }
73 DIHeaderFieldIterator &operator++() {
77 DIHeaderFieldIterator operator++(int) {
78 DIHeaderFieldIterator X(*this);
82 bool operator==(const DIHeaderFieldIterator &X) const {
83 return Current.data() == X.Current.data();
85 bool operator!=(const DIHeaderFieldIterator &X) const {
89 StringRef getHeader() const { return Header; }
90 StringRef getCurrent() const { return Current; }
91 StringRef getPrefix() const {
92 if (Current.begin() == Header.begin())
94 return Header.slice(0, Current.begin() - Header.begin() - 1);
96 StringRef getSuffix() const {
97 if (Current.end() == Header.end())
99 return Header.slice(Current.end() - Header.begin() + 1, StringRef::npos);
102 /// \brief Get the current field as a number.
104 /// Convert the current field into a number. Return \c 0 on error.
105 template <class T> T getNumber() const {
107 if (getCurrent().getAsInteger(0, Int))
114 assert(Current.data() != nullptr && "Cannot increment past the end");
115 StringRef Suffix = getSuffix();
116 Current = Suffix.slice(0, Suffix.find('\0'));
120 /// \brief A thin wraper around MDNode to access encoded debug info.
122 /// This should not be stored in a container, because the underlying MDNode may
123 /// change in certain situations.
125 // Befriends DIRef so DIRef can befriend the protected member
126 // function: getFieldAs<DIRef>.
127 template <typename T> friend class DIRef;
130 /// \brief Accessibility flags.
132 /// The three accessibility flags are mutually exclusive and rolled together
133 /// in the first two bits.
135 FlagAccessibility = 1 << 0 | 1 << 1,
140 FlagFwdDecl = 1 << 2,
141 FlagAppleBlock = 1 << 3,
142 FlagBlockByrefStruct = 1 << 4,
143 FlagVirtual = 1 << 5,
144 FlagArtificial = 1 << 6,
145 FlagExplicit = 1 << 7,
146 FlagPrototyped = 1 << 8,
147 FlagObjcClassComplete = 1 << 9,
148 FlagObjectPointer = 1 << 10,
149 FlagVector = 1 << 11,
150 FlagStaticMember = 1 << 12,
151 FlagLValueReference = 1 << 13,
152 FlagRValueReference = 1 << 14
156 const MDNode *DbgNode;
158 StringRef getStringField(unsigned Elt) const;
159 unsigned getUnsignedField(unsigned Elt) const {
160 return (unsigned)getUInt64Field(Elt);
162 uint64_t getUInt64Field(unsigned Elt) const;
163 int64_t getInt64Field(unsigned Elt) const;
164 DIDescriptor getDescriptorField(unsigned Elt) const;
166 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
167 return DescTy(getDescriptorField(Elt));
170 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
171 Constant *getConstantField(unsigned Elt) const;
172 Function *getFunctionField(unsigned Elt) const;
173 void replaceFunctionField(unsigned Elt, Function *F);
176 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
180 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
181 operator MDNode *() const { return get(); }
182 MDNode *operator->() const { return get(); }
184 // An explicit operator bool so that we can do testing of DI values
186 // FIXME: This operator bool isn't actually protecting anything at the
187 // moment due to the conversion operator above making DIDescriptor nodes
188 // implicitly convertable to bool.
189 LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
191 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
192 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
194 StringRef getHeader() const {
195 return getStringField(0);
198 size_t getNumHeaderFields() const {
199 return std::distance(DIHeaderFieldIterator(getHeader()),
200 DIHeaderFieldIterator());
203 DIHeaderFieldIterator header_begin() const { return getHeader(); }
204 DIHeaderFieldIterator header_end() const { return StringRef(); }
206 DIHeaderFieldIterator getHeaderIterator(unsigned Index) const {
207 // Since callers expect an empty string for out-of-range accesses, we can't
208 // use std::advance() here.
209 for (auto I = header_begin(), E = header_end(); I != E; ++I, --Index)
215 StringRef getHeaderField(unsigned Index) const {
216 return *getHeaderIterator(Index);
219 template <class T> T getHeaderFieldAs(unsigned Index) const {
220 return getHeaderIterator(Index).getNumber<T>();
223 uint16_t getTag() const { return getHeaderFieldAs<uint16_t>(0); }
225 bool isDerivedType() const;
226 bool isCompositeType() const;
227 bool isSubroutineType() const;
228 bool isBasicType() const;
229 bool isVariable() const;
230 bool isSubprogram() const;
231 bool isGlobalVariable() const;
232 bool isScope() const;
234 bool isCompileUnit() const;
235 bool isNameSpace() const;
236 bool isLexicalBlockFile() const;
237 bool isLexicalBlock() const;
238 bool isSubrange() const;
239 bool isEnumerator() const;
241 bool isTemplateTypeParameter() const;
242 bool isTemplateValueParameter() const;
243 bool isObjCProperty() const;
244 bool isImportedEntity() const;
245 bool isExpression() const;
247 void print(raw_ostream &OS) const;
250 /// \brief Replace all uses of debug info referenced by this descriptor.
251 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
252 void replaceAllUsesWith(MDNode *D);
255 /// \brief This is used to represent ranges, for array bounds.
256 class DISubrange : public DIDescriptor {
257 friend class DIDescriptor;
258 void printInternal(raw_ostream &OS) const;
261 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
263 int64_t getLo() const { return getHeaderFieldAs<int64_t>(1); }
264 int64_t getCount() const { return getHeaderFieldAs<int64_t>(2); }
268 /// \brief This descriptor holds an array of nodes with type T.
269 template <typename T> class DITypedArray : public DIDescriptor {
271 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
272 unsigned getNumElements() const {
273 return DbgNode ? DbgNode->getNumOperands() : 0;
275 T getElement(unsigned Idx) const {
276 return getFieldAs<T>(Idx);
280 typedef DITypedArray<DIDescriptor> DIArray;
282 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
284 /// FIXME: it seems strange that this doesn't have either a reference to the
285 /// type/precision or a file/line pair for location info.
286 class DIEnumerator : public DIDescriptor {
287 friend class DIDescriptor;
288 void printInternal(raw_ostream &OS) const;
291 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
293 StringRef getName() const { return getHeaderField(1); }
294 int64_t getEnumValue() const { return getHeaderFieldAs<int64_t>(2); }
298 template <typename T> class DIRef;
299 typedef DIRef<DIScope> DIScopeRef;
300 typedef DIRef<DIType> DITypeRef;
301 typedef DITypedArray<DITypeRef> DITypeArray;
303 /// \brief A base class for various scopes.
305 /// Although, implementation-wise, DIScope is the parent class of most
306 /// other DIxxx classes, including DIType and its descendants, most of
307 /// DIScope's descendants are not a substitutable subtype of
308 /// DIScope. The DIDescriptor::isScope() method only is true for
309 /// DIScopes that are scopes in the strict lexical scope sense
310 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
311 class DIScope : public DIDescriptor {
313 friend class DIDescriptor;
314 void printInternal(raw_ostream &OS) const;
317 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
319 /// \brief Get the parent scope.
321 /// Gets the parent scope for this scope node or returns a default
322 /// constructed scope.
323 DIScopeRef getContext() const;
324 /// \brief Get the scope name.
326 /// If the scope node has a name, return that, else return an empty string.
327 StringRef getName() const;
328 StringRef getFilename() const;
329 StringRef getDirectory() const;
331 /// \brief Generate a reference to this DIScope.
333 /// Uses the type identifier instead of the actual MDNode if possible, to
334 /// help type uniquing.
335 DIScopeRef getRef() const;
338 /// \brief Represents reference to a DIDescriptor.
340 /// Abstracts over direct and identifier-based metadata references.
341 template <typename T> class DIRef {
342 template <typename DescTy>
343 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
344 friend DIScopeRef DIScope::getContext() const;
345 friend DIScopeRef DIScope::getRef() const;
348 /// \brief Val can be either a MDNode or a MDString.
350 /// In the latter, MDString specifies the type identifier.
352 explicit DIRef(const Metadata *V);
355 T resolve(const DITypeIdentifierMap &Map) const;
356 StringRef getName() const;
357 operator Metadata *() const { return const_cast<Metadata *>(Val); }
360 template <typename T>
361 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
365 if (const MDNode *MD = dyn_cast<MDNode>(Val))
368 const MDString *MS = cast<MDString>(Val);
369 // Find the corresponding MDNode.
370 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
371 assert(Iter != Map.end() && "Identifier not in the type map?");
372 assert(DIDescriptor(Iter->second).isType() &&
373 "MDNode in DITypeIdentifierMap should be a DIType.");
374 return T(Iter->second);
377 template <typename T> StringRef DIRef<T>::getName() const {
381 if (const MDNode *MD = dyn_cast<MDNode>(Val))
382 return T(MD).getName();
384 const MDString *MS = cast<MDString>(Val);
385 return MS->getString();
388 /// \brief Handle fields that are references to DIScopes.
389 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
390 /// \brief Specialize DIRef constructor for DIScopeRef.
391 template <> DIRef<DIScope>::DIRef(const Metadata *V);
393 /// \brief Handle fields that are references to DITypes.
394 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
395 /// \brief Specialize DIRef constructor for DITypeRef.
396 template <> DIRef<DIType>::DIRef(const Metadata *V);
398 /// \briefThis is a wrapper for a type.
400 /// FIXME: Types should be factored much better so that CV qualifiers and
401 /// others do not require a huge and empty descriptor full of zeros.
402 class DIType : public DIScope {
404 friend class DIDescriptor;
405 void printInternal(raw_ostream &OS) const;
408 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
409 operator DITypeRef () const {
411 "constructing DITypeRef from an MDNode that is not a type");
412 return DITypeRef(&*getRef());
417 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
418 StringRef getName() const { return getHeaderField(1); }
419 unsigned getLineNumber() const {
420 return getHeaderFieldAs<unsigned>(2);
422 uint64_t getSizeInBits() const {
423 return getHeaderFieldAs<unsigned>(3);
425 uint64_t getAlignInBits() const {
426 return getHeaderFieldAs<unsigned>(4);
428 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
429 // carry this is just plain insane.
430 uint64_t getOffsetInBits() const {
431 return getHeaderFieldAs<unsigned>(5);
433 unsigned getFlags() const { return getHeaderFieldAs<unsigned>(6); }
434 bool isPrivate() const {
435 return (getFlags() & FlagAccessibility) == FlagPrivate;
437 bool isProtected() const {
438 return (getFlags() & FlagAccessibility) == FlagProtected;
440 bool isPublic() const {
441 return (getFlags() & FlagAccessibility) == FlagPublic;
443 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
444 bool isAppleBlockExtension() const {
445 return (getFlags() & FlagAppleBlock) != 0;
447 bool isBlockByrefStruct() const {
448 return (getFlags() & FlagBlockByrefStruct) != 0;
450 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
451 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
452 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
453 bool isObjcClassComplete() const {
454 return (getFlags() & FlagObjcClassComplete) != 0;
456 bool isVector() const { return (getFlags() & FlagVector) != 0; }
457 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
458 bool isLValueReference() const {
459 return (getFlags() & FlagLValueReference) != 0;
461 bool isRValueReference() const {
462 return (getFlags() & FlagRValueReference) != 0;
464 bool isValid() const { return DbgNode && isType(); }
467 /// \brief A basic type, like 'int' or 'float'.
468 class DIBasicType : public DIType {
470 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
472 unsigned getEncoding() const { return getHeaderFieldAs<unsigned>(7); }
477 /// \brief A simple derived type
479 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
480 /// Or, a data member of a class/struct/union.
481 class DIDerivedType : public DIType {
482 friend class DIDescriptor;
483 void printInternal(raw_ostream &OS) const;
486 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
488 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(3); }
490 /// \brief Return property node, if this ivar is associated with one.
491 MDNode *getObjCProperty() const;
493 DITypeRef getClassType() const {
494 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
495 return getFieldAs<DITypeRef>(4);
498 Constant *getConstant() const {
499 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
500 return getConstantField(4);
506 /// \brief Types that refer to multiple other types.
508 /// This descriptor holds a type that can refer to multiple other types, like a
509 /// function or struct.
511 /// DICompositeType is derived from DIDerivedType because some
512 /// composite types (such as enums) can be derived from basic types
513 // FIXME: Make this derive from DIType directly & just store the
514 // base type in a single DIType field.
515 class DICompositeType : public DIDerivedType {
516 friend class DIBuilder;
517 friend class DIDescriptor;
518 void printInternal(raw_ostream &OS) const;
520 /// \brief Set the array of member DITypes.
521 void setArraysHelper(MDNode *Elements, MDNode *TParams);
524 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
526 DIArray getElements() const {
527 assert(!isSubroutineType() && "no elements for DISubroutineType");
528 return getFieldAs<DIArray>(4);
532 template <typename T>
533 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
534 assert((!TParams || DbgNode->getNumOperands() == 8) &&
535 "If you're setting the template parameters this should include a slot "
537 setArraysHelper(Elements, TParams);
541 unsigned getRunTimeLang() const {
542 return getHeaderFieldAs<unsigned>(7);
544 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(5); }
547 /// \brief Set the containing type.
548 void setContainingType(DICompositeType ContainingType);
551 DIArray getTemplateParams() const { return getFieldAs<DIArray>(6); }
552 MDString *getIdentifier() const;
557 class DISubroutineType : public DICompositeType {
559 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
560 DITypedArray<DITypeRef> getTypeArray() const {
561 return getFieldAs<DITypedArray<DITypeRef>>(4);
565 /// \brief This is a wrapper for a file.
566 class DIFile : public DIScope {
567 friend class DIDescriptor;
570 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
572 /// \brief Retrieve the MDNode for the directory/file pair.
573 MDNode *getFileNode() const;
577 /// \brief A wrapper for a compile unit.
578 class DICompileUnit : public DIScope {
579 friend class DIDescriptor;
580 void printInternal(raw_ostream &OS) const;
583 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
585 dwarf::SourceLanguage getLanguage() const {
586 return static_cast<dwarf::SourceLanguage>(getHeaderFieldAs<unsigned>(1));
588 StringRef getProducer() const { return getHeaderField(2); }
590 bool isOptimized() const { return getHeaderFieldAs<bool>(3) != 0; }
591 StringRef getFlags() const { return getHeaderField(4); }
592 unsigned getRunTimeVersion() const { return getHeaderFieldAs<unsigned>(5); }
594 DIArray getEnumTypes() const;
595 DIArray getRetainedTypes() const;
596 DIArray getSubprograms() const;
597 DIArray getGlobalVariables() const;
598 DIArray getImportedEntities() const;
600 void replaceSubprograms(DIArray Subprograms);
601 void replaceGlobalVariables(DIArray GlobalVariables);
603 StringRef getSplitDebugFilename() const { return getHeaderField(6); }
604 unsigned getEmissionKind() const { return getHeaderFieldAs<unsigned>(7); }
609 /// \brief This is a wrapper for a subprogram (e.g. a function).
610 class DISubprogram : public DIScope {
611 friend class DIDescriptor;
612 void printInternal(raw_ostream &OS) const;
615 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
617 StringRef getName() const { return getHeaderField(1); }
618 StringRef getDisplayName() const { return getHeaderField(2); }
619 StringRef getLinkageName() const { return getHeaderField(3); }
620 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(4); }
622 /// \brief Check if this is local (like 'static' in C).
623 unsigned isLocalToUnit() const { return getHeaderFieldAs<unsigned>(5); }
624 unsigned isDefinition() const { return getHeaderFieldAs<unsigned>(6); }
626 unsigned getVirtuality() const { return getHeaderFieldAs<unsigned>(7); }
627 unsigned getVirtualIndex() const { return getHeaderFieldAs<unsigned>(8); }
629 unsigned getFlags() const { return getHeaderFieldAs<unsigned>(9); }
631 unsigned isOptimized() const { return getHeaderFieldAs<bool>(10); }
633 /// \brief Get the beginning of the scope of the function (not the name).
634 unsigned getScopeLineNumber() const { return getHeaderFieldAs<unsigned>(11); }
636 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
637 DISubroutineType getType() const { return getFieldAs<DISubroutineType>(3); }
639 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(4); }
643 /// \brief Check if this provides debugging information for the function F.
644 bool describes(const Function *F);
646 Function *getFunction() const { return getFunctionField(5); }
647 void replaceFunction(Function *F) { replaceFunctionField(5, F); }
648 DIArray getTemplateParams() const { return getFieldAs<DIArray>(6); }
649 DISubprogram getFunctionDeclaration() const {
650 return getFieldAs<DISubprogram>(7);
652 MDNode *getVariablesNodes() const;
653 DIArray getVariables() const;
655 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
656 /// \brief Check for the "private" access specifier.
657 bool isPrivate() const {
658 return (getFlags() & FlagAccessibility) == FlagPrivate;
660 /// \brief Check for the "protected" access specifier.
661 bool isProtected() const {
662 return (getFlags() & FlagAccessibility) == FlagProtected;
664 /// \brief Check for the "public" access specifier.
665 bool isPublic() const {
666 return (getFlags() & FlagAccessibility) == FlagPublic;
668 /// \brief Check for "explicit".
669 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
670 /// \brief Check if this is prototyped.
671 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
673 /// \brief Check if this is reference-qualified.
675 /// Return true if this subprogram is a C++11 reference-qualified non-static
676 /// member function (void foo() &).
677 unsigned isLValueReference() const {
678 return (getFlags() & FlagLValueReference) != 0;
681 /// \brief Check if this is rvalue-reference-qualified.
683 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
684 /// non-static member function (void foo() &&).
685 unsigned isRValueReference() const {
686 return (getFlags() & FlagRValueReference) != 0;
691 /// \brief This is a wrapper for a lexical block.
692 class DILexicalBlock : public DIScope {
694 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
695 DIScope getContext() const { return getFieldAs<DIScope>(2); }
696 unsigned getLineNumber() const {
697 return getHeaderFieldAs<unsigned>(1);
699 unsigned getColumnNumber() const {
700 return getHeaderFieldAs<unsigned>(2);
705 /// \brief This is a wrapper for a lexical block with a filename change.
706 class DILexicalBlockFile : public DIScope {
708 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
709 DIScope getContext() const {
710 if (getScope().isSubprogram())
712 return getScope().getContext();
714 unsigned getLineNumber() const { return getScope().getLineNumber(); }
715 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
716 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
717 unsigned getDiscriminator() const { return getHeaderFieldAs<unsigned>(1); }
721 /// \brief A wrapper for a C++ style name space.
722 class DINameSpace : public DIScope {
723 friend class DIDescriptor;
724 void printInternal(raw_ostream &OS) const;
727 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
728 StringRef getName() const { return getHeaderField(1); }
729 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
730 DIScope getContext() const { return getFieldAs<DIScope>(2); }
734 /// \brief This is a wrapper for template type parameter.
735 class DITemplateTypeParameter : public DIDescriptor {
737 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
740 StringRef getName() const { return getHeaderField(1); }
741 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
742 unsigned getColumnNumber() const { return getHeaderFieldAs<unsigned>(3); }
744 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
745 DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
746 StringRef getFilename() const { return getFieldAs<DIFile>(3).getFilename(); }
747 StringRef getDirectory() const {
748 return getFieldAs<DIFile>(3).getDirectory();
753 /// \brief This is a wrapper for template value parameter.
754 class DITemplateValueParameter : public DIDescriptor {
756 explicit DITemplateValueParameter(const MDNode *N = nullptr)
759 StringRef getName() const { return getHeaderField(1); }
760 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
761 unsigned getColumnNumber() const { return getHeaderFieldAs<unsigned>(3); }
763 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
764 DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
765 Metadata *getValue() const;
766 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
767 StringRef getDirectory() const {
768 return getFieldAs<DIFile>(4).getDirectory();
773 /// \brief This is a wrapper for a global variable.
774 class DIGlobalVariable : public DIDescriptor {
775 friend class DIDescriptor;
776 void printInternal(raw_ostream &OS) const;
779 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
781 StringRef getName() const { return getHeaderField(1); }
782 StringRef getDisplayName() const { return getHeaderField(2); }
783 StringRef getLinkageName() const { return getHeaderField(3); }
784 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(4); }
785 unsigned isLocalToUnit() const { return getHeaderFieldAs<bool>(5); }
786 unsigned isDefinition() const { return getHeaderFieldAs<bool>(6); }
788 DIScope getContext() const { return getFieldAs<DIScope>(1); }
789 StringRef getFilename() const { return getFieldAs<DIFile>(2).getFilename(); }
790 StringRef getDirectory() const {
791 return getFieldAs<DIFile>(2).getDirectory();
793 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
795 GlobalVariable *getGlobal() const { return getGlobalVariableField(4); }
796 Constant *getConstant() const { return getConstantField(4); }
797 DIDerivedType getStaticDataMemberDeclaration() const {
798 return getFieldAs<DIDerivedType>(5);
804 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
805 class DIVariable : public DIDescriptor {
806 friend class DIDescriptor;
807 void printInternal(raw_ostream &OS) const;
810 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
812 StringRef getName() const { return getHeaderField(1); }
813 unsigned getLineNumber() const {
814 // FIXME: Line number and arg number shouldn't be merged together like this.
815 return (getHeaderFieldAs<unsigned>(2) << 8) >> 8;
817 unsigned getArgNumber() const { return getHeaderFieldAs<unsigned>(2) >> 24; }
819 DIScope getContext() const { return getFieldAs<DIScope>(1); }
820 DIFile getFile() const { return getFieldAs<DIFile>(2); }
821 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
823 /// \brief Return true if this variable is marked as "artificial".
824 bool isArtificial() const {
825 return (getHeaderFieldAs<unsigned>(3) & FlagArtificial) != 0;
828 bool isObjectPointer() const {
829 return (getHeaderFieldAs<unsigned>(3) & FlagObjectPointer) != 0;
832 /// \brief If this variable is inlined then return inline location.
833 MDNode *getInlinedAt() const;
837 /// \brief Check if this is a "__block" variable (Apple Blocks).
838 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
839 return (getType().resolve(Map)).isBlockByrefStruct();
842 /// \brief Check if this is an inlined function argument.
843 bool isInlinedFnArgument(const Function *CurFn);
845 /// \brief Return the size reported by the variable's type.
846 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
848 void printExtendedName(raw_ostream &OS) const;
851 class DIExpressionIterator;
853 /// \brief A complex location expression.
854 class DIExpression : public DIDescriptor {
855 friend class DIDescriptor;
856 void printInternal(raw_ostream &OS) const;
859 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
863 /// \brief Return the number of elements in the complex expression.
864 unsigned getNumElements() const {
867 unsigned N = getNumHeaderFields();
868 assert(N > 0 && "missing tag");
872 /// \brief return the Idx'th complex address element.
873 uint64_t getElement(unsigned Idx) const;
875 /// \brief Return whether this is a piece of an aggregate variable.
876 bool isVariablePiece() const;
877 /// \brief Return the offset of this piece in bytes.
878 uint64_t getPieceOffset() const;
879 /// \brief Return the size of this piece in bytes.
880 uint64_t getPieceSize() const;
882 DIExpressionIterator begin() const;
883 DIExpressionIterator end() const;
886 /// \brief An iterator for DIExpression elments.
887 class DIExpressionIterator
888 : public std::iterator<std::forward_iterator_tag, StringRef, unsigned,
889 const uint64_t *, uint64_t> {
890 DIHeaderFieldIterator I;
891 DIExpressionIterator(DIHeaderFieldIterator I) : I(I) {}
893 DIExpressionIterator() {}
894 DIExpressionIterator(const DIExpression &Expr) : I(++Expr.header_begin()) {}
895 uint64_t operator*() const { return I.getNumber<uint64_t>(); }
896 DIExpressionIterator &operator++() {
900 DIExpressionIterator operator++(int) {
901 DIExpressionIterator X(*this);
905 bool operator==(const DIExpressionIterator &X) const {
908 bool operator!=(const DIExpressionIterator &X) const {
909 return !(*this == X);
912 uint64_t getArg(unsigned N) const {
915 return In.getNumber<uint64_t>();
918 const DIHeaderFieldIterator& getBase() const { return I; }
923 case dwarf::DW_OP_piece: std::advance(I, 3); break;
924 case dwarf::DW_OP_plus: std::advance(I, 2); break;
925 case dwarf::DW_OP_deref: std::advance(I, 1); break;
927 assert("unsupported operand");
933 /// \brief This object holds location information.
935 /// This object is not associated with any DWARF tag.
936 class DILocation : public DIDescriptor {
938 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
940 unsigned getLineNumber() const {
941 if (auto *L = dyn_cast_or_null<MDLocation>(DbgNode))
945 unsigned getColumnNumber() const {
946 if (auto *L = dyn_cast_or_null<MDLocation>(DbgNode))
947 return L->getColumn();
950 DIScope getScope() const {
951 if (auto *L = dyn_cast_or_null<MDLocation>(DbgNode))
952 return DIScope(dyn_cast_or_null<MDNode>(L->getScope()));
953 return DIScope(nullptr);
955 DILocation getOrigLocation() const {
956 if (auto *L = dyn_cast_or_null<MDLocation>(DbgNode))
957 return DILocation(dyn_cast_or_null<MDNode>(L->getInlinedAt()));
958 return DILocation(nullptr);
960 StringRef getFilename() const { return getScope().getFilename(); }
961 StringRef getDirectory() const { return getScope().getDirectory(); }
963 bool atSameLineAs(const DILocation &Other) const {
964 return (getLineNumber() == Other.getLineNumber() &&
965 getFilename() == Other.getFilename());
967 /// \brief Get the DWAF discriminator.
969 /// DWARF discriminators are used to distinguish identical file locations for
970 /// instructions that are on different basic blocks. If two instructions are
971 /// inside the same lexical block and are in different basic blocks, we
972 /// create a new lexical block with identical location as the original but
973 /// with a different discriminator value
974 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
975 unsigned getDiscriminator() const {
976 // Since discriminators are associated with lexical blocks, make
977 // sure this location is a lexical block before retrieving its
979 return getScope().isLexicalBlockFile()
980 ? DILexicalBlockFile(
981 cast<MDNode>(cast<MDLocation>(DbgNode)->getScope()))
986 /// \brief Generate a new discriminator value for this location.
987 unsigned computeNewDiscriminator(LLVMContext &Ctx);
989 /// \brief Return a copy of this location with a different scope.
990 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
993 class DIObjCProperty : public DIDescriptor {
994 friend class DIDescriptor;
995 void printInternal(raw_ostream &OS) const;
998 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
1000 StringRef getObjCPropertyName() const { return getHeaderField(1); }
1001 DIFile getFile() const { return getFieldAs<DIFile>(1); }
1002 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
1004 StringRef getObjCPropertyGetterName() const { return getHeaderField(3); }
1005 StringRef getObjCPropertySetterName() const { return getHeaderField(4); }
1006 unsigned getAttributes() const { return getHeaderFieldAs<unsigned>(5); }
1007 bool isReadOnlyObjCProperty() const {
1008 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
1010 bool isReadWriteObjCProperty() const {
1011 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
1013 bool isAssignObjCProperty() const {
1014 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
1016 bool isRetainObjCProperty() const {
1017 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
1019 bool isCopyObjCProperty() const {
1020 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
1022 bool isNonAtomicObjCProperty() const {
1023 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
1026 /// \brief Get the type.
1028 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
1029 /// the type as a DITypeRef here.
1030 DIType getType() const { return getFieldAs<DIType>(2); }
1032 bool Verify() const;
1035 /// \brief An imported module (C++ using directive or similar).
1036 class DIImportedEntity : public DIDescriptor {
1037 friend class DIDescriptor;
1038 void printInternal(raw_ostream &OS) const;
1041 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
1042 DIScope getContext() const { return getFieldAs<DIScope>(1); }
1043 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
1044 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(1); }
1045 StringRef getName() const { return getHeaderField(2); }
1046 bool Verify() const;
1049 /// \brief Find subprogram that is enclosing this scope.
1050 DISubprogram getDISubprogram(const MDNode *Scope);
1052 /// \brief Find debug info for a given function.
1053 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
1055 DISubprogram getDISubprogram(const Function *F);
1057 /// \brief Find underlying composite type.
1058 DICompositeType getDICompositeType(DIType T);
1060 /// \brief Create a new inlined variable based on current variable.
1062 /// @param DV Current Variable.
1063 /// @param InlinedScope Location at current variable is inlined.
1064 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
1065 LLVMContext &VMContext);
1067 /// \brief Remove inlined scope from the variable.
1068 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
1070 /// \brief Generate map by visiting all retained types.
1071 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
1073 /// \brief Strip debug info in the module if it exists.
1075 /// To do this, we remove all calls to the debugger intrinsics and any named
1076 /// metadata for debugging. We also remove debug locations for instructions.
1077 /// Return true if module is modified.
1078 bool StripDebugInfo(Module &M);
1080 /// \brief Return Debug Info Metadata Version by checking module flags.
1081 unsigned getDebugMetadataVersionFromModule(const Module &M);
1083 /// \brief Utility to find all debug info in a module.
1085 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1086 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1087 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1088 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1089 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1090 /// used by the CUs.
1091 class DebugInfoFinder {
1093 DebugInfoFinder() : TypeMapInitialized(false) {}
1095 /// \brief Process entire module and collect debug info anchors.
1096 void processModule(const Module &M);
1098 /// \brief Process DbgDeclareInst.
1099 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1100 /// \brief Process DbgValueInst.
1101 void processValue(const Module &M, const DbgValueInst *DVI);
1102 /// \brief Process DILocation.
1103 void processLocation(const Module &M, DILocation Loc);
1105 /// \brief Process DIExpression.
1106 void processExpression(DIExpression Expr);
1108 /// \brief Clear all lists.
1112 void InitializeTypeMap(const Module &M);
1114 void processType(DIType DT);
1115 void processSubprogram(DISubprogram SP);
1116 void processScope(DIScope Scope);
1117 bool addCompileUnit(DICompileUnit CU);
1118 bool addGlobalVariable(DIGlobalVariable DIG);
1119 bool addSubprogram(DISubprogram SP);
1120 bool addType(DIType DT);
1121 bool addScope(DIScope Scope);
1124 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1125 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1126 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
1127 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1128 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1130 iterator_range<compile_unit_iterator> compile_units() const {
1131 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1134 iterator_range<subprogram_iterator> subprograms() const {
1135 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1138 iterator_range<global_variable_iterator> global_variables() const {
1139 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1142 iterator_range<type_iterator> types() const {
1143 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1146 iterator_range<scope_iterator> scopes() const {
1147 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1150 unsigned compile_unit_count() const { return CUs.size(); }
1151 unsigned global_variable_count() const { return GVs.size(); }
1152 unsigned subprogram_count() const { return SPs.size(); }
1153 unsigned type_count() const { return TYs.size(); }
1154 unsigned scope_count() const { return Scopes.size(); }
1157 SmallVector<DICompileUnit, 8> CUs;
1158 SmallVector<DISubprogram, 8> SPs;
1159 SmallVector<DIGlobalVariable, 8> GVs;
1160 SmallVector<DIType, 8> TYs;
1161 SmallVector<DIScope, 8> Scopes;
1162 SmallPtrSet<MDNode *, 64> NodesSeen;
1163 DITypeIdentifierMap TypeIdentifierMap;
1165 /// \brief Specify if TypeIdentifierMap is initialized.
1166 bool TypeMapInitialized;
1169 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1171 } // end namespace llvm