LLVM 23.0.0git
DebugLoc.h
Go to the documentation of this file.
1//===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a number of light weight data structures used
10// to describe and track debug location information.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_DEBUGLOC_H
15#define LLVM_IR_DEBUGLOC_H
16
17#include "llvm/Config/llvm-config.h"
21
22namespace llvm {
23
24class LLVMContext;
25class raw_ostream;
26class DILocation;
27class Function;
28
29#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
30#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
31extern bool DebugLocOriginCollectionEnabled;
32
33struct DbgLocOrigin {
34 static constexpr unsigned long MaxDepth = 16;
35 using StackTracesTy =
36 SmallVector<std::pair<int, std::array<void *, MaxDepth>>, 0>;
37 StackTracesTy StackTraces;
38 DbgLocOrigin(bool ShouldCollectTrace);
39 void addTrace();
40 const StackTracesTy &getOriginStackTraces() const { return StackTraces; };
41};
42#else
43struct DbgLocOrigin {
44 DbgLocOrigin(bool) {}
45};
46#endif
47// Used to represent different "kinds" of DebugLoc, expressing that the
48// instruction it is part of is either normal and should contain a valid
49// DILocation, or otherwise describing the reason why the instruction does
50// not contain a valid DILocation.
51enum class DebugLocKind : uint8_t {
52 // The instruction is expected to contain a valid DILocation.
53 Normal,
54 // The instruction is compiler-generated, i.e. it is not associated with any
55 // line in the original source.
57 // The instruction has intentionally had its source location removed,
58 // typically because it was moved outside of its original control-flow and
59 // presenting the prior source location would be misleading for debuggers
60 // or profilers.
61 Dropped,
62 // The instruction does not have a known or currently knowable source
63 // location, e.g. the attribution is ambiguous in a way that can't be
64 // represented, or determining the correct location is complicated and
65 // requires future developer effort.
66 Unknown,
67 // DebugLoc is attached to an instruction that we don't expect to be
68 // emitted, and so can omit a valid DILocation; we don't expect to ever try
69 // and emit these into the line table, and trying to do so is a sign that
70 // something has gone wrong (most likely a DebugLoc leaking from a transient
71 // compiler-generated instruction).
72 Temporary
73};
74
75// Extends a DILocation pointer to also store a DebugLocKind and Origin,
76// allowing Debugify to ignore intentionally-empty DebugLocs and display the
77// code responsible for generating unintentionally-empty DebugLocs.
78// Currently we only need to track the Origin of this DILoc when using a
79// DebugLoc that is not annotated (i.e. has DebugLocKind::Normal) and has a
80// null DILocation, so only collect the origin stacktrace in those cases.
81class DILocAndCoverageTracking : public DbgLocOrigin {
82 DILocation *Loc;
83
84public:
85 DebugLocKind Kind;
86 // Default constructor for empty DebugLocs.
87 DILocAndCoverageTracking()
88 : DbgLocOrigin(true), Loc(nullptr), Kind(DebugLocKind::Normal) {}
89 // Valid or nullptr DILocation*, no annotative DebugLocKind.
90 DILocAndCoverageTracking(const DILocation *Loc)
91 : DbgLocOrigin(!Loc), Loc(const_cast<DILocation *>(Loc)),
92 Kind(DebugLocKind::Normal) {}
93 // Explicit DebugLocKind, which always means a nullptr DILocation*.
94 DILocAndCoverageTracking(DebugLocKind Kind)
95 : DbgLocOrigin(Kind == DebugLocKind::Normal), Loc(nullptr), Kind(Kind) {}
96
97 operator DILocation *() const { return Loc; }
98};
99template <> struct simplify_type<DILocAndCoverageTracking> {
100 using SimpleType = DILocation *;
101
102 static DILocation *getSimplifiedValue(DILocAndCoverageTracking &MD) {
103 return MD;
104 }
105};
106template <> struct simplify_type<const DILocAndCoverageTracking> {
107 using SimpleType = DILocation *;
108
109 static DILocation *getSimplifiedValue(const DILocAndCoverageTracking &MD) {
110 return MD;
111 }
112};
113
114using DebugLocRef = DILocAndCoverageTracking;
115#else
117#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
118
119/// A debug info location.
120///
121/// This class is a wrapper around an \a DILocation
122/// pointer.
123///
124/// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a
125/// one based on relatively opaque \a MDNode pointers.
126class DebugLoc {
127 DebugLocRef Loc = {};
128
129public:
130 /// Construct from an \a DILocation.
131 DebugLoc(const DILocation *L = nullptr) : Loc(const_cast<DILocation *>(L)) {}
132
133#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
134 DebugLoc(DebugLocKind Kind) : Loc(Kind) {}
135 DebugLocKind getKind() const { return Loc.Kind; }
136#endif
137
138#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
139 static inline DebugLoc getTemporary() {
140 return DebugLoc(DebugLocKind::Temporary);
141 }
142 static inline DebugLoc getUnknown() {
143 return DebugLoc(DebugLocKind::Unknown);
144 }
145 static inline DebugLoc getCompilerGenerated() {
146 return DebugLoc(DebugLocKind::CompilerGenerated);
147 }
148 static inline DebugLoc getDropped() {
149 return DebugLoc(DebugLocKind::Dropped);
150 }
151#else
152 static inline DebugLoc getTemporary() { return DebugLoc(); }
153 static inline DebugLoc getUnknown() { return DebugLoc(); }
154 static inline DebugLoc getCompilerGenerated() { return DebugLoc(); }
155 static inline DebugLoc getDropped() { return DebugLoc(); }
156#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
157
158 /// When two instructions are combined into a single instruction we also
159 /// need to combine the original locations into a single location.
160 /// When the locations are the same we can use either location.
161 /// When they differ, we need a third location which is distinct from
162 /// either. If they share a common scope, use this scope and compare the
163 /// line/column pair of the locations with the common scope:
164 /// * if both match, keep the line and column;
165 /// * if only the line number matches, keep the line and set the column as
166 /// 0;
167 /// * otherwise set line and column as 0.
168 /// If they do not share a common scope the location is ambiguous and can't
169 /// be represented in a line entry. In this case, set line and column as 0
170 /// and use the scope of any location.
171 ///
172 /// \p LocA \p LocB: The locations to be merged.
174
175 /// Try to combine the vector of locations passed as input in a single one.
176 /// This function applies getMergedLocation() repeatedly left-to-right.
177 ///
178 /// \p Locs: The locations to be merged.
180
181 /// If this DebugLoc is non-empty, returns this DebugLoc; otherwise, selects
182 /// \p Other.
183 /// In coverage-tracking builds, this also accounts for whether this or
184 /// \p Other have an annotative DebugLocKind applied, such that if both are
185 /// empty but exactly one has an annotation, we prefer that annotated
186 /// location.
188 if (*this)
189 return *this;
190#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
191 if (Other)
192 return Other;
193 if (getKind() != DebugLocKind::Normal)
194 return *this;
195 if (Other.getKind() != DebugLocKind::Normal)
196 return Other;
197 return *this;
198#else
199 return Other;
200#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
201 }
202
203#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
204 const DbgLocOrigin::StackTracesTy &getOriginStackTraces() const {
205 return Loc.getOriginStackTraces();
206 }
207 DebugLoc getCopied() const {
208 DebugLoc NewDL = *this;
209 NewDL.Loc.addTrace();
210 return NewDL;
211 }
212#else
213 DebugLoc getCopied() const { return *this; }
214#endif
215
216 /// Get the underlying \a DILocation.
217 ///
218 /// \pre !*this or \c isa<DILocation>(getAsMDNode()).
219 /// @{
220 DILocation *get() const { return Loc; }
221 operator DILocation *() const { return get(); }
222 DILocation *operator->() const { return get(); }
223 DILocation &operator*() const { return *get(); }
224 /// @}
225
226 /// Check for null.
227 ///
228 /// Check for null in a way that is safe with broken debug info. Unlike
229 /// the conversion to \c DILocation, this doesn't require that \c Loc is of
230 /// the right type. Important for cases like \a llvm::StripDebugInfo() and
231 /// \a Instruction::hasMetadata().
232 explicit operator bool() const { return Loc; }
233
234 enum { ReplaceLastInlinedAt = true };
235 /// Rebuild the entire inlined-at chain for this instruction so that the top
236 /// of the chain now is inlined-at the new call site.
237 /// \param InlinedAt The new outermost inlined-at in the chain.
238 LLVM_ABI static DebugLoc
239 appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt, LLVMContext &Ctx,
241
242 /// Return true if the source locations match, ignoring isImplicitCode and
243 /// source atom info.
245 if (get() == Other.get())
246 return true;
247 return ((bool)*this == (bool)Other) && getLine() == Other.getLine() &&
248 getCol() == Other.getCol() && getScope() == Other.getScope() &&
249 getInlinedAt() == Other.getInlinedAt();
250 }
251
252 LLVM_ABI unsigned getLine() const;
253 LLVM_ABI unsigned getCol() const;
254 LLVM_ABI MDNode *getScope() const;
256
257 /// Get the fully inlined-at scope for a DebugLoc.
258 ///
259 /// Gets the inlined-at scope for a DebugLoc.
261
262 /// Rebuild the entire inline-at chain by replacing the subprogram at the
263 /// end of the chain with NewSP.
264 LLVM_ABI static DebugLoc
266 LLVMContext &Ctx,
268
269 /// Find the debug info location for the start of the function.
270 ///
271 /// Walk up the scope chain of given debug loc and find line number info
272 /// for the function.
273 ///
274 /// FIXME: Remove this. Users should use DILocation/DILocalScope API to
275 /// find the subprogram, and then DILocation::get().
277
278 /// Return \c this as a bar \a MDNode.
279 LLVM_ABI MDNode *getAsMDNode() const;
280
281 /// Check if the DebugLoc corresponds to an implicit code.
282 LLVM_ABI bool isImplicitCode() const;
283 LLVM_ABI void setImplicitCode(bool ImplicitCode);
284
285 bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
286 bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; }
287
288 LLVM_ABI void dump() const;
289
290 /// prints source location /path/to/file.exe:line:col @[inlined at]
291 LLVM_ABI void print(raw_ostream &OS) const;
292};
293
294} // end namespace llvm
295
296#endif // LLVM_IR_DEBUGLOC_H
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
basic Basic Alias true
#define LLVM_ABI
Definition Compiler.h:215
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Subprogram description. Uses SubclassData1.
A debug info location.
Definition DebugLoc.h:126
LLVM_ABI void setImplicitCode(bool ImplicitCode)
Definition DebugLoc.cpp:84
DILocation & operator*() const
Definition DebugLoc.h:223
bool isSameSourceLocation(const DebugLoc &Other) const
Return true if the source locations match, ignoring isImplicitCode and source atom info.
Definition DebugLoc.h:244
static DebugLoc getCompilerGenerated()
Definition DebugLoc.h:154
bool operator==(const DebugLoc &DL) const
Definition DebugLoc.h:285
LLVM_ABI unsigned getLine() const
Definition DebugLoc.cpp:43
DebugLoc orElse(DebugLoc Other) const
If this DebugLoc is non-empty, returns this DebugLoc; otherwise, selects Other.
Definition DebugLoc.h:187
DILocation * get() const
Get the underlying DILocation.
Definition DebugLoc.h:220
LLVM_ABI DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition DebugLoc.cpp:67
LLVM_ABI MDNode * getScope() const
Definition DebugLoc.cpp:53
LLVM_ABI MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition DebugLoc.cpp:76
DebugLoc(const DILocation *L=nullptr)
Construct from an DILocation.
Definition DebugLoc.h:131
DebugLoc getCopied() const
Definition DebugLoc.h:213
static LLVM_ABI DebugLoc appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inlined-at chain for this instruction so that the top of the chain now is inlined-...
Definition DebugLoc.cpp:129
DILocation * operator->() const
Definition DebugLoc.h:222
static DebugLoc getTemporary()
Definition DebugLoc.h:152
static LLVM_ABI DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
Definition DebugLoc.cpp:172
LLVM_ABI void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition DebugLoc.cpp:193
static LLVM_ABI DebugLoc getMergedLocations(ArrayRef< DebugLoc > Locs)
Try to combine the vector of locations passed as input in a single one.
Definition DebugLoc.cpp:159
LLVM_ABI unsigned getCol() const
Definition DebugLoc.cpp:48
static LLVM_ABI DebugLoc replaceInlinedAtSubprogram(const DebugLoc &DL, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inline-at chain by replacing the subprogram at the end of the chain with NewSP.
Definition DebugLoc.cpp:89
LLVM_ABI bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition DebugLoc.cpp:78
LLVM_ABI void dump() const
Definition DebugLoc.cpp:190
static DebugLoc getUnknown()
Definition DebugLoc.h:153
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
static DebugLoc getDropped()
Definition DebugLoc.h:155
bool operator!=(const DebugLoc &DL) const
Definition DebugLoc.h:286
LLVM_ABI MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition DebugLoc.cpp:63
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1069
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
DILocation * DebugLocRef
Definition DebugLoc.h:116
@ Other
Any other memory.
Definition ModRef.h:68
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34
static SimpleType & getSimplifiedValue(From &Val)
Definition Casting.h:38