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/iterator_range.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.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::input_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);
104 assert(Current.data() != nullptr && "Cannot increment past the end");
105 StringRef Suffix = getSuffix();
106 Current = Suffix.slice(0, Suffix.find('\0'));
110 /// \brief A thin wraper around MDNode to access encoded debug info.
112 /// This should not be stored in a container, because the underlying MDNode may
113 /// change in certain situations.
115 // Befriends DIRef so DIRef can befriend the protected member
116 // function: getFieldAs<DIRef>.
117 template <typename T> friend class DIRef;
120 /// \brief Accessibility flags.
122 /// The three accessibility flags are mutually exclusive and rolled together
123 /// in the first two bits.
125 FlagAccessibility = 1 << 0 | 1 << 1,
130 FlagFwdDecl = 1 << 2,
131 FlagAppleBlock = 1 << 3,
132 FlagBlockByrefStruct = 1 << 4,
133 FlagVirtual = 1 << 5,
134 FlagArtificial = 1 << 6,
135 FlagExplicit = 1 << 7,
136 FlagPrototyped = 1 << 8,
137 FlagObjcClassComplete = 1 << 9,
138 FlagObjectPointer = 1 << 10,
139 FlagVector = 1 << 11,
140 FlagStaticMember = 1 << 12,
141 FlagIndirectVariable = 1 << 13,
142 FlagLValueReference = 1 << 14,
143 FlagRValueReference = 1 << 15
147 const MDNode *DbgNode;
149 StringRef getStringField(unsigned Elt) const;
150 unsigned getUnsignedField(unsigned Elt) const {
151 return (unsigned)getUInt64Field(Elt);
153 uint64_t getUInt64Field(unsigned Elt) const;
154 int64_t getInt64Field(unsigned Elt) const;
155 DIDescriptor getDescriptorField(unsigned Elt) const;
157 template <typename DescTy> DescTy getFieldAs(unsigned Elt) const {
158 return DescTy(getDescriptorField(Elt));
161 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
162 Constant *getConstantField(unsigned Elt) const;
163 Function *getFunctionField(unsigned Elt) const;
164 void replaceFunctionField(unsigned Elt, Function *F);
167 explicit DIDescriptor(const MDNode *N = nullptr) : DbgNode(N) {}
171 MDNode *get() const { return const_cast<MDNode *>(DbgNode); }
172 operator MDNode *() const { return get(); }
173 MDNode *operator->() const { return get(); }
175 // An explicit operator bool so that we can do testing of DI values
177 // FIXME: This operator bool isn't actually protecting anything at the
178 // moment due to the conversion operator above making DIDescriptor nodes
179 // implicitly convertable to bool.
180 LLVM_EXPLICIT operator bool() const { return DbgNode != nullptr; }
182 bool operator==(DIDescriptor Other) const { return DbgNode == Other.DbgNode; }
183 bool operator!=(DIDescriptor Other) const { return !operator==(Other); }
185 StringRef getHeader() const {
186 return getStringField(0);
189 size_t getNumHeaderFields() const {
190 return std::distance(DIHeaderFieldIterator(getHeader()),
191 DIHeaderFieldIterator());
194 StringRef getHeaderField(unsigned Index) const {
195 // Since callers expect an empty string for out-of-range accesses, we can't
196 // use std::advance() here.
197 for (DIHeaderFieldIterator I(getHeader()), E; I != E; ++I, --Index)
203 template <class T> T getHeaderFieldAs(unsigned Index) const {
205 if (getHeaderField(Index).getAsInteger(0, Int))
210 uint16_t getTag() const { return getHeaderFieldAs<uint16_t>(0); }
212 bool isDerivedType() const;
213 bool isCompositeType() const;
214 bool isSubroutineType() const;
215 bool isBasicType() const;
216 bool isVariable() const;
217 bool isSubprogram() const;
218 bool isGlobalVariable() const;
219 bool isScope() const;
221 bool isCompileUnit() const;
222 bool isNameSpace() const;
223 bool isLexicalBlockFile() const;
224 bool isLexicalBlock() const;
225 bool isSubrange() const;
226 bool isEnumerator() const;
228 bool isTemplateTypeParameter() const;
229 bool isTemplateValueParameter() const;
230 bool isObjCProperty() const;
231 bool isImportedEntity() const;
232 bool isExpression() const;
234 void print(raw_ostream &OS) const;
237 /// \brief Replace all uses of debug info referenced by this descriptor.
238 void replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D);
239 void replaceAllUsesWith(MDNode *D);
242 /// \brief This is used to represent ranges, for array bounds.
243 class DISubrange : public DIDescriptor {
244 friend class DIDescriptor;
245 void printInternal(raw_ostream &OS) const;
248 explicit DISubrange(const MDNode *N = nullptr) : DIDescriptor(N) {}
250 int64_t getLo() const { return getHeaderFieldAs<int64_t>(1); }
251 int64_t getCount() const { return getHeaderFieldAs<int64_t>(2); }
255 /// \brief This descriptor holds an array of nodes with type T.
256 template <typename T> class DITypedArray : public DIDescriptor {
258 explicit DITypedArray(const MDNode *N = nullptr) : DIDescriptor(N) {}
259 unsigned getNumElements() const {
260 return DbgNode ? DbgNode->getNumOperands() : 0;
262 T getElement(unsigned Idx) const {
263 return getFieldAs<T>(Idx);
267 typedef DITypedArray<DIDescriptor> DIArray;
269 /// \brief A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
271 /// FIXME: it seems strange that this doesn't have either a reference to the
272 /// type/precision or a file/line pair for location info.
273 class DIEnumerator : public DIDescriptor {
274 friend class DIDescriptor;
275 void printInternal(raw_ostream &OS) const;
278 explicit DIEnumerator(const MDNode *N = nullptr) : DIDescriptor(N) {}
280 StringRef getName() const { return getHeaderField(1); }
281 int64_t getEnumValue() const { return getHeaderFieldAs<int64_t>(2); }
285 template <typename T> class DIRef;
286 typedef DIRef<DIScope> DIScopeRef;
287 typedef DIRef<DIType> DITypeRef;
288 typedef DITypedArray<DITypeRef> DITypeArray;
290 /// \brief A base class for various scopes.
292 /// Although, implementation-wise, DIScope is the parent class of most
293 /// other DIxxx classes, including DIType and its descendants, most of
294 /// DIScope's descendants are not a substitutable subtype of
295 /// DIScope. The DIDescriptor::isScope() method only is true for
296 /// DIScopes that are scopes in the strict lexical scope sense
297 /// (DICompileUnit, DISubprogram, etc.), but not for, e.g., a DIType.
298 class DIScope : public DIDescriptor {
300 friend class DIDescriptor;
301 void printInternal(raw_ostream &OS) const;
304 explicit DIScope(const MDNode *N = nullptr) : DIDescriptor(N) {}
306 /// \brief Get the parent scope.
308 /// Gets the parent scope for this scope node or returns a default
309 /// constructed scope.
310 DIScopeRef getContext() const;
311 /// \brief Get the scope name.
313 /// If the scope node has a name, return that, else return an empty string.
314 StringRef getName() const;
315 StringRef getFilename() const;
316 StringRef getDirectory() const;
318 /// \brief Generate a reference to this DIScope.
320 /// Uses the type identifier instead of the actual MDNode if possible, to
321 /// help type uniquing.
322 DIScopeRef getRef() const;
325 /// \brief Represents reference to a DIDescriptor.
327 /// Abstracts over direct and identifier-based metadata references.
328 template <typename T> class DIRef {
329 template <typename DescTy>
330 friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const;
331 friend DIScopeRef DIScope::getContext() const;
332 friend DIScopeRef DIScope::getRef() const;
335 /// \brief Val can be either a MDNode or a MDString.
337 /// In the latter, MDString specifies the type identifier.
339 explicit DIRef(const Metadata *V);
342 T resolve(const DITypeIdentifierMap &Map) const;
343 StringRef getName() const;
344 operator Metadata *() const { return const_cast<Metadata *>(Val); }
347 template <typename T>
348 T DIRef<T>::resolve(const DITypeIdentifierMap &Map) const {
352 if (const MDNode *MD = dyn_cast<MDNode>(Val))
355 const MDString *MS = cast<MDString>(Val);
356 // Find the corresponding MDNode.
357 DITypeIdentifierMap::const_iterator Iter = Map.find(MS);
358 assert(Iter != Map.end() && "Identifier not in the type map?");
359 assert(DIDescriptor(Iter->second).isType() &&
360 "MDNode in DITypeIdentifierMap should be a DIType.");
361 return T(Iter->second);
364 template <typename T> StringRef DIRef<T>::getName() const {
368 if (const MDNode *MD = dyn_cast<MDNode>(Val))
369 return T(MD).getName();
371 const MDString *MS = cast<MDString>(Val);
372 return MS->getString();
375 /// \brief Handle fields that are references to DIScopes.
376 template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
377 /// \brief Specialize DIRef constructor for DIScopeRef.
378 template <> DIRef<DIScope>::DIRef(const Metadata *V);
380 /// \brief Handle fields that are references to DITypes.
381 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
382 /// \brief Specialize DIRef constructor for DITypeRef.
383 template <> DIRef<DIType>::DIRef(const Metadata *V);
385 /// \briefThis is a wrapper for a type.
387 /// FIXME: Types should be factored much better so that CV qualifiers and
388 /// others do not require a huge and empty descriptor full of zeros.
389 class DIType : public DIScope {
391 friend class DIDescriptor;
392 void printInternal(raw_ostream &OS) const;
395 explicit DIType(const MDNode *N = nullptr) : DIScope(N) {}
396 operator DITypeRef () const {
398 "constructing DITypeRef from an MDNode that is not a type");
399 return DITypeRef(&*getRef());
404 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
405 StringRef getName() const { return getHeaderField(1); }
406 unsigned getLineNumber() const {
407 return getHeaderFieldAs<unsigned>(2);
409 uint64_t getSizeInBits() const {
410 return getHeaderFieldAs<unsigned>(3);
412 uint64_t getAlignInBits() const {
413 return getHeaderFieldAs<unsigned>(4);
415 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
416 // carry this is just plain insane.
417 uint64_t getOffsetInBits() const {
418 return getHeaderFieldAs<unsigned>(5);
420 unsigned getFlags() const { return getHeaderFieldAs<unsigned>(6); }
421 bool isPrivate() const {
422 return (getFlags() & FlagAccessibility) == FlagPrivate;
424 bool isProtected() const {
425 return (getFlags() & FlagAccessibility) == FlagProtected;
427 bool isPublic() const {
428 return (getFlags() & FlagAccessibility) == FlagPublic;
430 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
431 bool isAppleBlockExtension() const {
432 return (getFlags() & FlagAppleBlock) != 0;
434 bool isBlockByrefStruct() const {
435 return (getFlags() & FlagBlockByrefStruct) != 0;
437 bool isVirtual() const { return (getFlags() & FlagVirtual) != 0; }
438 bool isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
439 bool isObjectPointer() const { return (getFlags() & FlagObjectPointer) != 0; }
440 bool isObjcClassComplete() const {
441 return (getFlags() & FlagObjcClassComplete) != 0;
443 bool isVector() const { return (getFlags() & FlagVector) != 0; }
444 bool isStaticMember() const { return (getFlags() & FlagStaticMember) != 0; }
445 bool isLValueReference() const {
446 return (getFlags() & FlagLValueReference) != 0;
448 bool isRValueReference() const {
449 return (getFlags() & FlagRValueReference) != 0;
451 bool isValid() const { return DbgNode && isType(); }
454 /// \brief A basic type, like 'int' or 'float'.
455 class DIBasicType : public DIType {
457 explicit DIBasicType(const MDNode *N = nullptr) : DIType(N) {}
459 unsigned getEncoding() const { return getHeaderFieldAs<unsigned>(7); }
464 /// \brief A simple derived type
466 /// Like a const qualified type, a typedef, a pointer or reference, et cetera.
467 /// Or, a data member of a class/struct/union.
468 class DIDerivedType : public DIType {
469 friend class DIDescriptor;
470 void printInternal(raw_ostream &OS) const;
473 explicit DIDerivedType(const MDNode *N = nullptr) : DIType(N) {}
475 DITypeRef getTypeDerivedFrom() const { return getFieldAs<DITypeRef>(3); }
477 /// \brief Return property node, if this ivar is associated with one.
478 MDNode *getObjCProperty() const;
480 DITypeRef getClassType() const {
481 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type);
482 return getFieldAs<DITypeRef>(4);
485 Constant *getConstant() const {
486 assert((getTag() == dwarf::DW_TAG_member) && isStaticMember());
487 return getConstantField(4);
493 /// \brief Types that refer to multiple other types.
495 /// This descriptor holds a type that can refer to multiple other types, like a
496 /// function or struct.
498 /// DICompositeType is derived from DIDerivedType because some
499 /// composite types (such as enums) can be derived from basic types
500 // FIXME: Make this derive from DIType directly & just store the
501 // base type in a single DIType field.
502 class DICompositeType : public DIDerivedType {
503 friend class DIDescriptor;
504 void printInternal(raw_ostream &OS) const;
506 /// \brief Set the array of member DITypes.
507 void setArraysHelper(MDNode *Elements, MDNode *TParams);
510 explicit DICompositeType(const MDNode *N = nullptr) : DIDerivedType(N) {}
512 DIArray getElements() const {
513 assert(!isSubroutineType() && "no elements for DISubroutineType");
514 return getFieldAs<DIArray>(4);
516 template <typename T>
517 void setArrays(DITypedArray<T> Elements, DIArray TParams = DIArray()) {
518 assert((!TParams || DbgNode->getNumOperands() == 8) &&
519 "If you're setting the template parameters this should include a slot "
521 setArraysHelper(Elements, TParams);
523 unsigned getRunTimeLang() const {
524 return getHeaderFieldAs<unsigned>(7);
526 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(5); }
528 /// \brief Set the containing type.
529 void setContainingType(DICompositeType ContainingType);
530 DIArray getTemplateParams() const { return getFieldAs<DIArray>(6); }
531 MDString *getIdentifier() const;
536 class DISubroutineType : public DICompositeType {
538 explicit DISubroutineType(const MDNode *N = nullptr) : DICompositeType(N) {}
539 DITypedArray<DITypeRef> getTypeArray() const {
540 return getFieldAs<DITypedArray<DITypeRef>>(4);
544 /// \brief This is a wrapper for a file.
545 class DIFile : public DIScope {
546 friend class DIDescriptor;
549 explicit DIFile(const MDNode *N = nullptr) : DIScope(N) {}
551 /// \brief Retrieve the MDNode for the directory/file pair.
552 MDNode *getFileNode() const;
556 /// \brief A wrapper for a compile unit.
557 class DICompileUnit : public DIScope {
558 friend class DIDescriptor;
559 void printInternal(raw_ostream &OS) const;
562 explicit DICompileUnit(const MDNode *N = nullptr) : DIScope(N) {}
564 dwarf::SourceLanguage getLanguage() const {
565 return static_cast<dwarf::SourceLanguage>(getHeaderFieldAs<unsigned>(1));
567 StringRef getProducer() const { return getHeaderField(2); }
569 bool isOptimized() const { return getHeaderFieldAs<bool>(3) != 0; }
570 StringRef getFlags() const { return getHeaderField(4); }
571 unsigned getRunTimeVersion() const { return getHeaderFieldAs<unsigned>(5); }
573 DIArray getEnumTypes() const;
574 DIArray getRetainedTypes() const;
575 DIArray getSubprograms() const;
576 DIArray getGlobalVariables() const;
577 DIArray getImportedEntities() const;
579 void replaceSubprograms(DIArray Subprograms);
580 void replaceGlobalVariables(DIArray GlobalVariables);
582 StringRef getSplitDebugFilename() const { return getHeaderField(6); }
583 unsigned getEmissionKind() const { return getHeaderFieldAs<unsigned>(7); }
588 /// \brief This is a wrapper for a subprogram (e.g. a function).
589 class DISubprogram : public DIScope {
590 friend class DIDescriptor;
591 void printInternal(raw_ostream &OS) const;
594 explicit DISubprogram(const MDNode *N = nullptr) : DIScope(N) {}
596 StringRef getName() const { return getHeaderField(1); }
597 StringRef getDisplayName() const { return getHeaderField(2); }
598 StringRef getLinkageName() const { return getHeaderField(3); }
599 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(4); }
601 /// \brief Check if this is local (like 'static' in C).
602 unsigned isLocalToUnit() const { return getHeaderFieldAs<unsigned>(5); }
603 unsigned isDefinition() const { return getHeaderFieldAs<unsigned>(6); }
605 unsigned getVirtuality() const { return getHeaderFieldAs<unsigned>(7); }
606 unsigned getVirtualIndex() const { return getHeaderFieldAs<unsigned>(8); }
608 unsigned getFlags() const { return getHeaderFieldAs<unsigned>(9); }
610 unsigned isOptimized() const { return getHeaderFieldAs<bool>(10); }
612 /// \brief Get the beginning of the scope of the function (not the name).
613 unsigned getScopeLineNumber() const { return getHeaderFieldAs<unsigned>(11); }
615 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(2); }
616 DISubroutineType getType() const { return getFieldAs<DISubroutineType>(3); }
618 DITypeRef getContainingType() const { return getFieldAs<DITypeRef>(4); }
622 /// \brief Check if this provides debugging information for the function F.
623 bool describes(const Function *F);
625 Function *getFunction() const { return getFunctionField(5); }
626 void replaceFunction(Function *F) { replaceFunctionField(5, F); }
627 DIArray getTemplateParams() const { return getFieldAs<DIArray>(6); }
628 DISubprogram getFunctionDeclaration() const {
629 return getFieldAs<DISubprogram>(7);
631 MDNode *getVariablesNodes() const;
632 DIArray getVariables() const;
634 unsigned isArtificial() const { return (getFlags() & FlagArtificial) != 0; }
635 /// \brief Check for the "private" access specifier.
636 bool isPrivate() const {
637 return (getFlags() & FlagAccessibility) == FlagPrivate;
639 /// \brief Check for the "protected" access specifier.
640 bool isProtected() const {
641 return (getFlags() & FlagAccessibility) == FlagProtected;
643 /// \brief Check for the "public" access specifier.
644 bool isPublic() const {
645 return (getFlags() & FlagAccessibility) == FlagPublic;
647 /// \brief Check for "explicit".
648 bool isExplicit() const { return (getFlags() & FlagExplicit) != 0; }
649 /// \brief Check if this is prototyped.
650 bool isPrototyped() const { return (getFlags() & FlagPrototyped) != 0; }
652 /// \brief Check if this is reference-qualified.
654 /// Return true if this subprogram is a C++11 reference-qualified non-static
655 /// member function (void foo() &).
656 unsigned isLValueReference() const {
657 return (getFlags() & FlagLValueReference) != 0;
660 /// \brief Check if this is rvalue-reference-qualified.
662 /// Return true if this subprogram is a C++11 rvalue-reference-qualified
663 /// non-static member function (void foo() &&).
664 unsigned isRValueReference() const {
665 return (getFlags() & FlagRValueReference) != 0;
670 /// \brief This is a wrapper for a lexical block.
671 class DILexicalBlock : public DIScope {
673 explicit DILexicalBlock(const MDNode *N = nullptr) : DIScope(N) {}
674 DIScope getContext() const { return getFieldAs<DIScope>(2); }
675 unsigned getLineNumber() const {
676 return getHeaderFieldAs<unsigned>(1);
678 unsigned getColumnNumber() const {
679 return getHeaderFieldAs<unsigned>(2);
684 /// \brief This is a wrapper for a lexical block with a filename change.
685 class DILexicalBlockFile : public DIScope {
687 explicit DILexicalBlockFile(const MDNode *N = nullptr) : DIScope(N) {}
688 DIScope getContext() const {
689 if (getScope().isSubprogram())
691 return getScope().getContext();
693 unsigned getLineNumber() const { return getScope().getLineNumber(); }
694 unsigned getColumnNumber() const { return getScope().getColumnNumber(); }
695 DILexicalBlock getScope() const { return getFieldAs<DILexicalBlock>(2); }
696 unsigned getDiscriminator() const { return getHeaderFieldAs<unsigned>(1); }
700 /// \brief A wrapper for a C++ style name space.
701 class DINameSpace : public DIScope {
702 friend class DIDescriptor;
703 void printInternal(raw_ostream &OS) const;
706 explicit DINameSpace(const MDNode *N = nullptr) : DIScope(N) {}
707 StringRef getName() const { return getHeaderField(1); }
708 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
709 DIScope getContext() const { return getFieldAs<DIScope>(2); }
713 /// \brief This is a wrapper for template type parameter.
714 class DITemplateTypeParameter : public DIDescriptor {
716 explicit DITemplateTypeParameter(const MDNode *N = nullptr)
719 StringRef getName() const { return getHeaderField(1); }
720 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
721 unsigned getColumnNumber() const { return getHeaderFieldAs<unsigned>(3); }
723 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
724 DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
725 StringRef getFilename() const { return getFieldAs<DIFile>(3).getFilename(); }
726 StringRef getDirectory() const {
727 return getFieldAs<DIFile>(3).getDirectory();
732 /// \brief This is a wrapper for template value parameter.
733 class DITemplateValueParameter : public DIDescriptor {
735 explicit DITemplateValueParameter(const MDNode *N = nullptr)
738 StringRef getName() const { return getHeaderField(1); }
739 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
740 unsigned getColumnNumber() const { return getHeaderFieldAs<unsigned>(3); }
742 DIScopeRef getContext() const { return getFieldAs<DIScopeRef>(1); }
743 DITypeRef getType() const { return getFieldAs<DITypeRef>(2); }
744 Metadata *getValue() const;
745 StringRef getFilename() const { return getFieldAs<DIFile>(4).getFilename(); }
746 StringRef getDirectory() const {
747 return getFieldAs<DIFile>(4).getDirectory();
752 /// \brief This is a wrapper for a global variable.
753 class DIGlobalVariable : public DIDescriptor {
754 friend class DIDescriptor;
755 void printInternal(raw_ostream &OS) const;
758 explicit DIGlobalVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
760 StringRef getName() const { return getHeaderField(1); }
761 StringRef getDisplayName() const { return getHeaderField(2); }
762 StringRef getLinkageName() const { return getHeaderField(3); }
763 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(4); }
764 unsigned isLocalToUnit() const { return getHeaderFieldAs<bool>(5); }
765 unsigned isDefinition() const { return getHeaderFieldAs<bool>(6); }
767 DIScope getContext() const { return getFieldAs<DIScope>(1); }
768 StringRef getFilename() const { return getFieldAs<DIFile>(2).getFilename(); }
769 StringRef getDirectory() const {
770 return getFieldAs<DIFile>(2).getDirectory();
772 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
774 GlobalVariable *getGlobal() const { return getGlobalVariableField(4); }
775 Constant *getConstant() const { return getConstantField(4); }
776 DIDerivedType getStaticDataMemberDeclaration() const {
777 return getFieldAs<DIDerivedType>(5);
783 /// \brief This is a wrapper for a variable (e.g. parameter, local, global etc).
784 class DIVariable : public DIDescriptor {
785 friend class DIDescriptor;
786 void printInternal(raw_ostream &OS) const;
789 explicit DIVariable(const MDNode *N = nullptr) : DIDescriptor(N) {}
791 StringRef getName() const { return getHeaderField(1); }
792 unsigned getLineNumber() const {
793 // FIXME: Line number and arg number shouldn't be merged together like this.
794 return (getHeaderFieldAs<unsigned>(2) << 8) >> 8;
796 unsigned getArgNumber() const { return getHeaderFieldAs<unsigned>(2) >> 24; }
798 DIScope getContext() const { return getFieldAs<DIScope>(1); }
799 DIFile getFile() const { return getFieldAs<DIFile>(2); }
800 DITypeRef getType() const { return getFieldAs<DITypeRef>(3); }
802 /// \brief Return true if this variable is marked as "artificial".
803 bool isArtificial() const {
804 return (getHeaderFieldAs<unsigned>(3) & FlagArtificial) != 0;
807 bool isObjectPointer() const {
808 return (getHeaderFieldAs<unsigned>(3) & FlagObjectPointer) != 0;
811 /// \brief Return true if this variable is represented as a pointer.
812 bool isIndirect() const {
813 return (getHeaderFieldAs<unsigned>(3) & FlagIndirectVariable) != 0;
816 /// \brief If this variable is inlined then return inline location.
817 MDNode *getInlinedAt() const;
821 /// \brief Check if this is a "__block" variable (Apple Blocks).
822 bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
823 return (getType().resolve(Map)).isBlockByrefStruct();
826 /// \brief Check if this is an inlined function argument.
827 bool isInlinedFnArgument(const Function *CurFn);
829 /// \brief Return the size reported by the variable's type.
830 unsigned getSizeInBits(const DITypeIdentifierMap &Map);
832 void printExtendedName(raw_ostream &OS) const;
835 /// \brief A complex location expression.
836 class DIExpression : public DIDescriptor {
837 friend class DIDescriptor;
838 void printInternal(raw_ostream &OS) const;
841 explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
845 /// \brief Return the number of elements in the complex expression.
846 unsigned getNumElements() const {
849 unsigned N = getNumHeaderFields();
850 assert(N > 0 && "missing tag");
854 /// \brief return the Idx'th complex address element.
855 uint64_t getElement(unsigned Idx) const;
857 /// \brief Return whether this is a piece of an aggregate variable.
858 bool isVariablePiece() const;
859 /// \brief Return the offset of this piece in bytes.
860 uint64_t getPieceOffset() const;
861 /// \brief Return the size of this piece in bytes.
862 uint64_t getPieceSize() const;
865 /// \brief This object holds location information.
867 /// This object is not associated with any DWARF tag.
868 class DILocation : public DIDescriptor {
870 explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
872 unsigned getLineNumber() const { return getUnsignedField(0); }
873 unsigned getColumnNumber() const { return getUnsignedField(1); }
874 DIScope getScope() const { return getFieldAs<DIScope>(2); }
875 DILocation getOrigLocation() const { return getFieldAs<DILocation>(3); }
876 StringRef getFilename() const { return getScope().getFilename(); }
877 StringRef getDirectory() const { return getScope().getDirectory(); }
879 bool atSameLineAs(const DILocation &Other) const {
880 return (getLineNumber() == Other.getLineNumber() &&
881 getFilename() == Other.getFilename());
883 /// \brief Get the DWAF discriminator.
885 /// DWARF discriminators are used to distinguish identical file locations for
886 /// instructions that are on different basic blocks. If two instructions are
887 /// inside the same lexical block and are in different basic blocks, we
888 /// create a new lexical block with identical location as the original but
889 /// with a different discriminator value
890 /// (lib/Transforms/Util/AddDiscriminators.cpp for details).
891 unsigned getDiscriminator() const {
892 // Since discriminators are associated with lexical blocks, make
893 // sure this location is a lexical block before retrieving its
895 return getScope().isLexicalBlockFile()
896 ? getFieldAs<DILexicalBlockFile>(2).getDiscriminator()
900 /// \brief Generate a new discriminator value for this location.
901 unsigned computeNewDiscriminator(LLVMContext &Ctx);
903 /// \brief Return a copy of this location with a different scope.
904 DILocation copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope);
907 class DIObjCProperty : public DIDescriptor {
908 friend class DIDescriptor;
909 void printInternal(raw_ostream &OS) const;
912 explicit DIObjCProperty(const MDNode *N) : DIDescriptor(N) {}
914 StringRef getObjCPropertyName() const { return getHeaderField(1); }
915 DIFile getFile() const { return getFieldAs<DIFile>(1); }
916 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(2); }
918 StringRef getObjCPropertyGetterName() const { return getHeaderField(3); }
919 StringRef getObjCPropertySetterName() const { return getHeaderField(4); }
920 unsigned getAttributes() const { return getHeaderFieldAs<unsigned>(5); }
921 bool isReadOnlyObjCProperty() const {
922 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readonly) != 0;
924 bool isReadWriteObjCProperty() const {
925 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_readwrite) != 0;
927 bool isAssignObjCProperty() const {
928 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_assign) != 0;
930 bool isRetainObjCProperty() const {
931 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_retain) != 0;
933 bool isCopyObjCProperty() const {
934 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_copy) != 0;
936 bool isNonAtomicObjCProperty() const {
937 return (getAttributes() & dwarf::DW_APPLE_PROPERTY_nonatomic) != 0;
940 /// \brief Get the type.
942 /// \note Objective-C doesn't have an ODR, so there is no benefit in storing
943 /// the type as a DITypeRef here.
944 DIType getType() const { return getFieldAs<DIType>(2); }
949 /// \brief An imported module (C++ using directive or similar).
950 class DIImportedEntity : public DIDescriptor {
951 friend class DIDescriptor;
952 void printInternal(raw_ostream &OS) const;
955 explicit DIImportedEntity(const MDNode *N) : DIDescriptor(N) {}
956 DIScope getContext() const { return getFieldAs<DIScope>(1); }
957 DIScopeRef getEntity() const { return getFieldAs<DIScopeRef>(2); }
958 unsigned getLineNumber() const { return getHeaderFieldAs<unsigned>(1); }
959 StringRef getName() const { return getHeaderField(2); }
963 /// \brief Find subprogram that is enclosing this scope.
964 DISubprogram getDISubprogram(const MDNode *Scope);
966 /// \brief Find debug info for a given function.
967 /// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
969 DISubprogram getDISubprogram(const Function *F);
971 /// \brief Find underlying composite type.
972 DICompositeType getDICompositeType(DIType T);
974 /// \brief Create a new inlined variable based on current variable.
976 /// @param DV Current Variable.
977 /// @param InlinedScope Location at current variable is inlined.
978 DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
979 LLVMContext &VMContext);
981 /// \brief Remove inlined scope from the variable.
982 DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);
984 /// \brief Generate map by visiting all retained types.
985 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
987 /// \brief Strip debug info in the module if it exists.
989 /// To do this, we remove all calls to the debugger intrinsics and any named
990 /// metadata for debugging. We also remove debug locations for instructions.
991 /// Return true if module is modified.
992 bool StripDebugInfo(Module &M);
994 /// \brief Return Debug Info Metadata Version by checking module flags.
995 unsigned getDebugMetadataVersionFromModule(const Module &M);
997 /// \brief Utility to find all debug info in a module.
999 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
1000 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
1001 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
1002 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
1003 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
1004 /// used by the CUs.
1005 class DebugInfoFinder {
1007 DebugInfoFinder() : TypeMapInitialized(false) {}
1009 /// \brief Process entire module and collect debug info anchors.
1010 void processModule(const Module &M);
1012 /// \brief Process DbgDeclareInst.
1013 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
1014 /// \brief Process DbgValueInst.
1015 void processValue(const Module &M, const DbgValueInst *DVI);
1016 /// \brief Process DILocation.
1017 void processLocation(const Module &M, DILocation Loc);
1019 /// \brief Clear all lists.
1023 void InitializeTypeMap(const Module &M);
1025 void processType(DIType DT);
1026 void processSubprogram(DISubprogram SP);
1027 void processScope(DIScope Scope);
1028 bool addCompileUnit(DICompileUnit CU);
1029 bool addGlobalVariable(DIGlobalVariable DIG);
1030 bool addSubprogram(DISubprogram SP);
1031 bool addType(DIType DT);
1032 bool addScope(DIScope Scope);
1035 typedef SmallVectorImpl<DICompileUnit>::const_iterator compile_unit_iterator;
1036 typedef SmallVectorImpl<DISubprogram>::const_iterator subprogram_iterator;
1037 typedef SmallVectorImpl<DIGlobalVariable>::const_iterator global_variable_iterator;
1038 typedef SmallVectorImpl<DIType>::const_iterator type_iterator;
1039 typedef SmallVectorImpl<DIScope>::const_iterator scope_iterator;
1041 iterator_range<compile_unit_iterator> compile_units() const {
1042 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
1045 iterator_range<subprogram_iterator> subprograms() const {
1046 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
1049 iterator_range<global_variable_iterator> global_variables() const {
1050 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
1053 iterator_range<type_iterator> types() const {
1054 return iterator_range<type_iterator>(TYs.begin(), TYs.end());
1057 iterator_range<scope_iterator> scopes() const {
1058 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
1061 unsigned compile_unit_count() const { return CUs.size(); }
1062 unsigned global_variable_count() const { return GVs.size(); }
1063 unsigned subprogram_count() const { return SPs.size(); }
1064 unsigned type_count() const { return TYs.size(); }
1065 unsigned scope_count() const { return Scopes.size(); }
1068 SmallVector<DICompileUnit, 8> CUs;
1069 SmallVector<DISubprogram, 8> SPs;
1070 SmallVector<DIGlobalVariable, 8> GVs;
1071 SmallVector<DIType, 8> TYs;
1072 SmallVector<DIScope, 8> Scopes;
1073 SmallPtrSet<MDNode *, 64> NodesSeen;
1074 DITypeIdentifierMap TypeIdentifierMap;
1076 /// \brief Specify if TypeIdentifierMap is initialized.
1077 bool TypeMapInitialized;
1080 DenseMap<const Function *, DISubprogram> makeSubprogramMap(const Module &M);
1082 } // end namespace llvm