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