1//===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
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#include "llvm/IR/DebugLoc.h"
10#include "llvm/Config/llvm-config.h"
11#include "llvm/IR/DebugInfo.h"
12
13using namespace llvm;
14
15#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
16#include "llvm/Support/Signals.h"
17namespace llvm {
18bool DebugLocOriginCollectionEnabled = false;
19} // namespace llvm
20
21DbgLocOrigin::DbgLocOrigin(bool ShouldCollectTrace) {
22 if (!ShouldCollectTrace || !DebugLocOriginCollectionEnabled)
23 return;
24 auto &[Depth, StackTrace] = StackTraces.emplace_back();
25 Depth = sys::getStackTrace(StackTrace);
26}
27void DbgLocOrigin::addTrace() {
28 // We only want to add new stacktraces if we already have one: addTrace exists
29 // to provide more context to how missing DebugLocs have propagated through
30 // the program, but by design if there is no existing stacktrace then we have
31 // decided not to track this DebugLoc as being "missing".
32 if (StackTraces.empty())
33 return;
34 auto &[Depth, StackTrace] = StackTraces.emplace_back();
35 Depth = sys::getStackTrace(StackTrace);
36}
37#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
38
39//===----------------------------------------------------------------------===//
40// DebugLoc Implementation
41//===----------------------------------------------------------------------===//
42
43unsigned DebugLoc::getLine() const {
44 assert(get() && "Expected valid DebugLoc");
45 return get()->getLine();
46}
47
48unsigned DebugLoc::getCol() const {
49 assert(get() && "Expected valid DebugLoc");
50 return get()->getColumn();
51}
52
53MDNode *DebugLoc::getScope() const {
54 assert(get() && "Expected valid DebugLoc");
55 return get()->getScope();
56}
57
58DILocation *DebugLoc::getInlinedAt() const {
59 assert(get() && "Expected valid DebugLoc");
60 return get()->getInlinedAt();
61}
62
63MDNode *DebugLoc::getInlinedAtScope() const {
64 return cast<DILocation>(Val: Loc)->getInlinedAtScope();
65}
66
67DebugLoc DebugLoc::getFnDebugLoc() const {
68 // FIXME: Add a method on \a DILocation that does this work.
69 const MDNode *Scope = getInlinedAtScope();
70 if (auto *SP = getDISubprogram(Scope))
71 return DILocation::get(Context&: SP->getContext(), Line: SP->getScopeLine(), Column: 0, Scope: SP);
72
73 return DebugLoc();
74}
75
76MDNode *DebugLoc::getAsMDNode() const { return Loc; }
77
78bool DebugLoc::isImplicitCode() const {
79 if (DILocation *Loc = get())
80 return Loc->isImplicitCode();
81 return true;
82}
83
84void DebugLoc::setImplicitCode(bool ImplicitCode) {
85 if (DILocation *Loc = get())
86 Loc->setImplicitCode(ImplicitCode);
87}
88
89DebugLoc DebugLoc::replaceInlinedAtSubprogram(
90 const DebugLoc &RootLoc, DISubprogram &NewSP, LLVMContext &Ctx,
91 DenseMap<const MDNode *, MDNode *> &Cache) {
92 SmallVector<DILocation *> LocChain;
93 DILocation *CachedResult = nullptr;
94
95 // Collect the inline chain, stopping if we find a location that has already
96 // been processed.
97 for (DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
98 if (auto It = Cache.find(Val: Loc); It != Cache.end()) {
99 CachedResult = cast<DILocation>(Val: It->second);
100 break;
101 }
102 LocChain.push_back(Elt: Loc);
103 }
104
105 DILocation *UpdatedLoc = CachedResult;
106 if (!UpdatedLoc) {
107 // If no cache hits, then back() is the end of the inline chain, that is,
108 // the DILocation whose scope ends in the Subprogram to be replaced.
109 DILocation *LocToUpdate = LocChain.pop_back_val();
110 DIScope *NewScope = DILocalScope::cloneScopeForSubprogram(
111 RootScope&: *LocToUpdate->getScope(), NewSP, Ctx, Cache);
112 UpdatedLoc = DILocation::get(Context&: Ctx, Line: LocToUpdate->getLine(),
113 Column: LocToUpdate->getColumn(), Scope: NewScope);
114 Cache[LocToUpdate] = UpdatedLoc;
115 }
116
117 // Recreate the location chain, bottom-up, starting at the new scope (or a
118 // cached result).
119 for (const DILocation *LocToUpdate : reverse(C&: LocChain)) {
120 UpdatedLoc =
121 DILocation::get(Context&: Ctx, Line: LocToUpdate->getLine(), Column: LocToUpdate->getColumn(),
122 Scope: LocToUpdate->getScope(), InlinedAt: UpdatedLoc);
123 Cache[LocToUpdate] = UpdatedLoc;
124 }
125
126 return UpdatedLoc;
127}
128
129DebugLoc DebugLoc::appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt,
130 LLVMContext &Ctx,
131 DenseMap<const MDNode *, MDNode *> &Cache) {
132 SmallVector<DILocation *, 3> InlinedAtLocations;
133 DILocation *Last = InlinedAt;
134 DILocation *CurInlinedAt = DL;
135
136 // Gather all the inlined-at nodes.
137 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
138 // Skip any we've already built nodes for.
139 if (auto *Found = Cache[IA]) {
140 Last = cast<DILocation>(Val: Found);
141 break;
142 }
143
144 InlinedAtLocations.push_back(Elt: IA);
145 CurInlinedAt = IA;
146 }
147
148 // Starting from the top, rebuild the nodes to point to the new inlined-at
149 // location (then rebuilding the rest of the chain behind it) and update the
150 // map of already-constructed inlined-at nodes.
151 // Key Instructions: InlinedAt fields don't need atom info.
152 for (const DILocation *MD : reverse(C&: InlinedAtLocations))
153 Cache[MD] = Last = DILocation::getDistinct(
154 Context&: Ctx, Line: MD->getLine(), Column: MD->getColumn(), Scope: MD->getScope(), InlinedAt: Last);
155
156 return Last;
157}
158
159DebugLoc DebugLoc::getMergedLocations(ArrayRef<DebugLoc> Locs) {
160 if (Locs.empty())
161 return DebugLoc();
162 if (Locs.size() == 1)
163 return Locs[0];
164 DebugLoc Merged = Locs[0];
165 for (const DebugLoc &DL : llvm::drop_begin(RangeOrContainer&: Locs)) {
166 Merged = getMergedLocation(LocA: Merged, LocB: DL);
167 if (!Merged)
168 break;
169 }
170 return Merged;
171}
172DebugLoc DebugLoc::getMergedLocation(DebugLoc LocA, DebugLoc LocB) {
173 if (!LocA || !LocB) {
174 // If coverage tracking is enabled, prioritize returning empty non-annotated
175 // locations to empty annotated locations.
176#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
177 if (!LocA && LocA.getKind() == DebugLocKind::Normal)
178 return LocA;
179 if (!LocB && LocB.getKind() == DebugLocKind::Normal)
180 return LocB;
181#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
182 if (!LocA)
183 return LocA;
184 return LocB;
185 }
186 return DILocation::getMergedLocation(LocA, LocB);
187}
188
189#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
190LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
191#endif
192
193void DebugLoc::print(raw_ostream &OS) const {
194 if (!Loc)
195 return;
196
197 // Print source line info.
198 auto *Scope = cast<DIScope>(Val: getScope());
199 OS << Scope->getFilename();
200 OS << ':' << getLine();
201 if (getCol() != 0)
202 OS << ':' << getCol();
203
204 if (DebugLoc InlinedAtDL = getInlinedAt()) {
205 OS << " @[ ";
206 InlinedAtDL.print(OS);
207 OS << " ]";
208 }
209}
210