1//===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
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
7//===----------------------------------------------------------------------===//
10#include "llvm/Config/llvm-config.h"
15#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
18DbgLocOrigin::DbgLocOrigin(
bool ShouldCollectTrace) {
19 if (!ShouldCollectTrace)
21 auto &[
Depth, StackTrace] = StackTraces.emplace_back();
22 Depth = sys::getStackTrace(StackTrace);
24void DbgLocOrigin::addTrace() {
25 // We only want to add new stacktraces if we already have one: addTrace exists
26 // to provide more context to how missing DebugLocs have propagated through
27 // the program, but by design if there is no existing stacktrace then we have
28 // decided not to track this DebugLoc as being "missing".
29 if (StackTraces.empty())
31 auto &[
Depth, StackTrace] = StackTraces.emplace_back();
32 Depth = sys::getStackTrace(StackTrace);
36#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
37DILocAndCoverageTracking::DILocAndCoverageTracking(
const DILocation *L)
39 Kind(DebugLocKind::
Normal) {}
40#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
42//===----------------------------------------------------------------------===//
43// DebugLoc Implementation
44//===----------------------------------------------------------------------===//
53 assert(
get() &&
"Expected valid DebugLoc");
54 return get()->getLine();
58 assert(
get() &&
"Expected valid DebugLoc");
59 return get()->getColumn();
63 assert(
get() &&
"Expected valid DebugLoc");
64 return get()->getScope();
68 assert(
get() &&
"Expected valid DebugLoc");
69 return get()->getInlinedAt();
77 // FIXME: Add a method on \a DILocation that does this work.
87 return Loc->isImplicitCode();
94 Loc->setImplicitCode(ImplicitCode);
104 // Collect the inline chain, stopping if we find a location that has already
106 for (
DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
107 if (
auto It = Cache.find(Loc); It != Cache.end()) {
116 // If no cache hits, then back() is the end of the inline chain, that is,
117 // the DILocation whose scope ends in the Subprogram to be replaced.
120 *LocToUpdate->getScope(), NewSP, Ctx, Cache);
122 LocToUpdate->getColumn(), NewScope);
123 Cache[LocToUpdate] = UpdatedLoc;
126 // Recreate the location chain, bottom-up, starting at the new scope (or a
131 LocToUpdate->getScope(), UpdatedLoc);
132 Cache[LocToUpdate] = UpdatedLoc;
145 // Gather all the inlined-at nodes.
146 while (
DILocation *IA = CurInlinedAt->getInlinedAt()) {
147 // Skip any we've already built nodes for.
148 if (
auto *Found = Cache[IA]) {
157 // Starting from the top, rebuild the nodes to point to the new inlined-at
158 // location (then rebuilding the rest of the chain behind it) and update the
159 // map of already-constructed inlined-at nodes.
160 // Key Instructions: InlinedAt fields don't need atom info.
163 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(),
Last);
171 if (Locs.
size() == 1)
182 if (!LocA || !LocB) {
183 // If coverage tracking is enabled, prioritize returning empty non-annotated
184 // locations to empty annotated locations.
185#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
186 if (!LocA && LocA.getKind() == DebugLocKind::Normal)
188 if (!LocB && LocB.getKind() == DebugLocKind::Normal)
190#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
198#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
206 // Print source line info.
208 OS << Scope->getFilename();
215 InlinedAtDL.print(OS);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
static LLVM_ABI DILocalScope * cloneScopeForSubprogram(DILocalScope &RootScope, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Traverses the scope chain rooted at RootScope until it hits a Subprogram, recreating the chain with "...
static LLVM_ABI DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
Attempts to merge LocA and LocB into a single location; see DebugLoc::getMergedLocation for more deta...
Base class for scope-like contexts.
Subprogram description. Uses SubclassData1.
LLVM_ABI void setImplicitCode(bool ImplicitCode)
LLVM_ABI unsigned getLine() const
LLVM_ABI DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
LLVM_ABI DILocation * get() const
Get the underlying DILocation.
LLVM_ABI MDNode * getScope() const
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-...
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...
LLVM_ABI void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
static LLVM_ABI DebugLoc getMergedLocations(ArrayRef< DebugLoc > Locs)
Try to combine the vector of locations passed as input in a single one.
LLVM_ABI unsigned getCol() const
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.
LLVM_ABI bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
LLVM_ABI void dump() const
LLVM_ABI DILocation * getInlinedAt() const
LLVM_ABI MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
This is an important class for using LLVM in a threaded context.
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
TypedTrackingMDRef< MDNode > TrackingMDNodeRef
auto cast_or_null(const Y &Val)
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.