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