xref: /freebsd/contrib/llvm-project/llvm/lib/IR/DebugLoc.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
10*0b57cec5SDimitry Andric #include "LLVMContextImpl.h"
11*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
12*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h"
13*0b57cec5SDimitry Andric using namespace llvm;
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
16*0b57cec5SDimitry Andric // DebugLoc Implementation
17*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18*0b57cec5SDimitry Andric DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
19*0b57cec5SDimitry Andric DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric DILocation *DebugLoc::get() const {
22*0b57cec5SDimitry Andric   return cast_or_null<DILocation>(Loc.get());
23*0b57cec5SDimitry Andric }
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric unsigned DebugLoc::getLine() const {
26*0b57cec5SDimitry Andric   assert(get() && "Expected valid DebugLoc");
27*0b57cec5SDimitry Andric   return get()->getLine();
28*0b57cec5SDimitry Andric }
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric unsigned DebugLoc::getCol() const {
31*0b57cec5SDimitry Andric   assert(get() && "Expected valid DebugLoc");
32*0b57cec5SDimitry Andric   return get()->getColumn();
33*0b57cec5SDimitry Andric }
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric MDNode *DebugLoc::getScope() const {
36*0b57cec5SDimitry Andric   assert(get() && "Expected valid DebugLoc");
37*0b57cec5SDimitry Andric   return get()->getScope();
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric DILocation *DebugLoc::getInlinedAt() const {
41*0b57cec5SDimitry Andric   assert(get() && "Expected valid DebugLoc");
42*0b57cec5SDimitry Andric   return get()->getInlinedAt();
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric MDNode *DebugLoc::getInlinedAtScope() const {
46*0b57cec5SDimitry Andric   return cast<DILocation>(Loc)->getInlinedAtScope();
47*0b57cec5SDimitry Andric }
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric DebugLoc DebugLoc::getFnDebugLoc() const {
50*0b57cec5SDimitry Andric   // FIXME: Add a method on \a DILocation that does this work.
51*0b57cec5SDimitry Andric   const MDNode *Scope = getInlinedAtScope();
52*0b57cec5SDimitry Andric   if (auto *SP = getDISubprogram(Scope))
53*0b57cec5SDimitry Andric     return DebugLoc::get(SP->getScopeLine(), 0, SP);
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric   return DebugLoc();
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric bool DebugLoc::isImplicitCode() const {
59*0b57cec5SDimitry Andric   if (DILocation *Loc = get()) {
60*0b57cec5SDimitry Andric     return Loc->isImplicitCode();
61*0b57cec5SDimitry Andric   }
62*0b57cec5SDimitry Andric   return true;
63*0b57cec5SDimitry Andric }
64*0b57cec5SDimitry Andric 
65*0b57cec5SDimitry Andric void DebugLoc::setImplicitCode(bool ImplicitCode) {
66*0b57cec5SDimitry Andric   if (DILocation *Loc = get()) {
67*0b57cec5SDimitry Andric     Loc->setImplicitCode(ImplicitCode);
68*0b57cec5SDimitry Andric   }
69*0b57cec5SDimitry Andric }
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
72*0b57cec5SDimitry Andric                        const MDNode *InlinedAt, bool ImplicitCode) {
73*0b57cec5SDimitry Andric   // If no scope is available, this is an unknown location.
74*0b57cec5SDimitry Andric   if (!Scope)
75*0b57cec5SDimitry Andric     return DebugLoc();
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric   return DILocation::get(Scope->getContext(), Line, Col,
78*0b57cec5SDimitry Andric                          const_cast<MDNode *>(Scope),
79*0b57cec5SDimitry Andric                          const_cast<MDNode *>(InlinedAt), ImplicitCode);
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt,
83*0b57cec5SDimitry Andric                                    LLVMContext &Ctx,
84*0b57cec5SDimitry Andric                                    DenseMap<const MDNode *, MDNode *> &Cache,
85*0b57cec5SDimitry Andric                                    bool ReplaceLast) {
86*0b57cec5SDimitry Andric   SmallVector<DILocation *, 3> InlinedAtLocations;
87*0b57cec5SDimitry Andric   DILocation *Last = InlinedAt;
88*0b57cec5SDimitry Andric   DILocation *CurInlinedAt = DL;
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric   // Gather all the inlined-at nodes.
91*0b57cec5SDimitry Andric   while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
92*0b57cec5SDimitry Andric     // Skip any we've already built nodes for.
93*0b57cec5SDimitry Andric     if (auto *Found = Cache[IA]) {
94*0b57cec5SDimitry Andric       Last = cast<DILocation>(Found);
95*0b57cec5SDimitry Andric       break;
96*0b57cec5SDimitry Andric     }
97*0b57cec5SDimitry Andric 
98*0b57cec5SDimitry Andric     if (ReplaceLast && !IA->getInlinedAt())
99*0b57cec5SDimitry Andric       break;
100*0b57cec5SDimitry Andric     InlinedAtLocations.push_back(IA);
101*0b57cec5SDimitry Andric     CurInlinedAt = IA;
102*0b57cec5SDimitry Andric   }
103*0b57cec5SDimitry Andric 
104*0b57cec5SDimitry Andric   // Starting from the top, rebuild the nodes to point to the new inlined-at
105*0b57cec5SDimitry Andric   // location (then rebuilding the rest of the chain behind it) and update the
106*0b57cec5SDimitry Andric   // map of already-constructed inlined-at nodes.
107*0b57cec5SDimitry Andric   for (const DILocation *MD : reverse(InlinedAtLocations))
108*0b57cec5SDimitry Andric     Cache[MD] = Last = DILocation::getDistinct(
109*0b57cec5SDimitry Andric         Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
110*0b57cec5SDimitry Andric 
111*0b57cec5SDimitry Andric   return Last;
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
115*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
116*0b57cec5SDimitry Andric #endif
117*0b57cec5SDimitry Andric 
118*0b57cec5SDimitry Andric void DebugLoc::print(raw_ostream &OS) const {
119*0b57cec5SDimitry Andric   if (!Loc)
120*0b57cec5SDimitry Andric     return;
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric   // Print source line info.
123*0b57cec5SDimitry Andric   auto *Scope = cast<DIScope>(getScope());
124*0b57cec5SDimitry Andric   OS << Scope->getFilename();
125*0b57cec5SDimitry Andric   OS << ':' << getLine();
126*0b57cec5SDimitry Andric   if (getCol() != 0)
127*0b57cec5SDimitry Andric     OS << ':' << getCol();
128*0b57cec5SDimitry Andric 
129*0b57cec5SDimitry Andric   if (DebugLoc InlinedAtDL = getInlinedAt()) {
130*0b57cec5SDimitry Andric     OS << " @[ ";
131*0b57cec5SDimitry Andric     InlinedAtDL.print(OS);
132*0b57cec5SDimitry Andric     OS << " ]";
133*0b57cec5SDimitry Andric   }
134*0b57cec5SDimitry Andric }
135