Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a number of light weight data structures used |
| 11 | // to describe and track debug location information. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_IR_DEBUGLOC_H |
| 16 | #define LLVM_IR_DEBUGLOC_H |
| 17 | |
| 18 | #include "llvm/IR/TrackingMDRef.h" |
| 19 | #include "llvm/Support/DataTypes.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class LLVMContext; |
| 24 | class raw_ostream; |
| 25 | class DILocation; |
| 26 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 27 | /// A debug info location. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 28 | /// |
| 29 | /// This class is a wrapper around a tracking reference to an \a DILocation |
| 30 | /// pointer. |
| 31 | /// |
| 32 | /// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a |
| 33 | /// one based on relatively opaque \a MDNode pointers. |
| 34 | class DebugLoc { |
| 35 | TrackingMDNodeRef Loc; |
| 36 | |
| 37 | public: |
| 38 | DebugLoc() = default; |
| 39 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | /// Construct from an \a DILocation. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | DebugLoc(const DILocation *L); |
| 42 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 43 | /// Construct from an \a MDNode. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | /// |
| 45 | /// Note: if \c N is not an \a DILocation, a verifier check will fail, and |
| 46 | /// accessors will crash. However, construction from other nodes is |
| 47 | /// supported in order to handle forward references when reading textual |
| 48 | /// IR. |
| 49 | explicit DebugLoc(const MDNode *N); |
| 50 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | /// Get the underlying \a DILocation. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// |
| 53 | /// \pre !*this or \c isa<DILocation>(getAsMDNode()). |
| 54 | /// @{ |
| 55 | DILocation *get() const; |
| 56 | operator DILocation *() const { return get(); } |
| 57 | DILocation *operator->() const { return get(); } |
| 58 | DILocation &operator*() const { return *get(); } |
| 59 | /// @} |
| 60 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 61 | /// Check for null. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 62 | /// |
| 63 | /// Check for null in a way that is safe with broken debug info. Unlike |
| 64 | /// the conversion to \c DILocation, this doesn't require that \c Loc is of |
| 65 | /// the right type. Important for cases like \a llvm::StripDebugInfo() and |
| 66 | /// \a Instruction::hasMetadata(). |
| 67 | explicit operator bool() const { return Loc; } |
| 68 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 69 | /// Check whether this has a trivial destructor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); } |
| 71 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 72 | /// Create a new DebugLoc. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | /// |
| 74 | /// Create a new DebugLoc at the specified line/col and scope/inline. This |
| 75 | /// forwards to \a DILocation::get(). |
| 76 | /// |
| 77 | /// If \c !Scope, returns a default-constructed \a DebugLoc. |
| 78 | /// |
| 79 | /// FIXME: Remove this. Users should use DILocation::get(). |
| 80 | static DebugLoc get(unsigned Line, unsigned Col, const MDNode *Scope, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 81 | const MDNode *InlinedAt = nullptr, |
| 82 | bool ImplicitCode = false); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | |
| 84 | enum { ReplaceLastInlinedAt = true }; |
| 85 | /// Rebuild the entire inlined-at chain for this instruction so that the top of |
| 86 | /// the chain now is inlined-at the new call site. |
| 87 | /// \param InlinedAt The new outermost inlined-at in the chain. |
| 88 | /// \param ReplaceLast Replace the last location in the inlined-at chain. |
| 89 | static DebugLoc appendInlinedAt(DebugLoc DL, DILocation *InlinedAt, |
| 90 | LLVMContext &Ctx, |
| 91 | DenseMap<const MDNode *, MDNode *> &Cache, |
| 92 | bool ReplaceLast = false); |
| 93 | |
| 94 | unsigned getLine() const; |
| 95 | unsigned getCol() const; |
| 96 | MDNode *getScope() const; |
| 97 | DILocation *getInlinedAt() const; |
| 98 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 99 | /// Get the fully inlined-at scope for a DebugLoc. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 100 | /// |
| 101 | /// Gets the inlined-at scope for a DebugLoc. |
| 102 | MDNode *getInlinedAtScope() const; |
| 103 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 104 | /// Find the debug info location for the start of the function. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 105 | /// |
| 106 | /// Walk up the scope chain of given debug loc and find line number info |
| 107 | /// for the function. |
| 108 | /// |
| 109 | /// FIXME: Remove this. Users should use DILocation/DILocalScope API to |
| 110 | /// find the subprogram, and then DILocation::get(). |
| 111 | DebugLoc getFnDebugLoc() const; |
| 112 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 113 | /// Return \c this as a bar \a MDNode. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 114 | MDNode *getAsMDNode() const { return Loc; } |
| 115 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 116 | /// Check if the DebugLoc corresponds to an implicit code. |
| 117 | bool isImplicitCode() const; |
| 118 | void setImplicitCode(bool ImplicitCode); |
| 119 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 120 | bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; } |
| 121 | bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; } |
| 122 | |
| 123 | void dump() const; |
| 124 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 125 | /// prints source location /path/to/file.exe:line:col @[inlined at] |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | void print(raw_ostream &OS) const; |
| 127 | }; |
| 128 | |
| 129 | } // end namespace llvm |
| 130 | |
| 131 | #endif /* LLVM_SUPPORT_DEBUGLOC_H */ |