LLVM  9.0.0svn
DebugLoc.cpp
Go to the documentation of this file.
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 
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 
36  assert(get() && "Expected valid DebugLoc");
37  return get()->getScope();
38 }
39 
41  assert(get() && "Expected valid DebugLoc");
42  return get()->getInlinedAt();
43 }
44 
46  return cast<DILocation>(Loc)->getInlinedAtScope();
47 }
48 
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 DebugLoc::get(SP->getScopeLine(), 0, SP);
54 
55  return DebugLoc();
56 }
57 
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::get(unsigned Line, unsigned Col, const MDNode *Scope,
72  const MDNode *InlinedAt, bool ImplicitCode) {
73  // If no scope is available, this is an unknown location.
74  if (!Scope)
75  return DebugLoc();
76 
77  return DILocation::get(Scope->getContext(), Line, Col,
78  const_cast<MDNode *>(Scope),
79  const_cast<MDNode *>(InlinedAt), ImplicitCode);
80 }
81 
83  LLVMContext &Ctx,
85  bool ReplaceLast) {
86  SmallVector<DILocation *, 3> InlinedAtLocations;
87  DILocation *Last = InlinedAt;
88  DILocation *CurInlinedAt = DL;
89 
90  // Gather all the inlined-at nodes.
91  while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
92  // Skip any we've already built nodes for.
93  if (auto *Found = Cache[IA]) {
94  Last = cast<DILocation>(Found);
95  break;
96  }
97 
98  if (ReplaceLast && !IA->getInlinedAt())
99  break;
100  InlinedAtLocations.push_back(IA);
101  CurInlinedAt = IA;
102  }
103 
104  // Starting from the top, rebuild the nodes to point to the new inlined-at
105  // location (then rebuilding the rest of the chain behind it) and update the
106  // map of already-constructed inlined-at nodes.
107  for (const DILocation *MD : reverse(InlinedAtLocations))
108  Cache[MD] = Last = DILocation::getDistinct(
109  Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
110 
111  return Last;
112 }
113 
114 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
116 #endif
117 
118 void DebugLoc::print(raw_ostream &OS) const {
119  if (!Loc)
120  return;
121 
122  // Print source line info.
123  auto *Scope = cast<DIScope>(getScope());
124  OS << Scope->getFilename();
125  OS << ':' << getLine();
126  if (getCol() != 0)
127  OS << ':' << getCol();
128 
129  if (DebugLoc InlinedAtDL = getInlinedAt()) {
130  OS << " @[ ";
131  InlinedAtDL.print(OS);
132  OS << " ]";
133  }
134 }
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:464
static DebugLoc appendInlinedAt(DebugLoc DL, DILocation *InlinedAt, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode *> &Cache, bool ReplaceLast=false)
Rebuild the entire inlined-at chain for this instruction so that the top of the chain now is inlined-...
Definition: DebugLoc.cpp:82
unsigned getLine() const
Definition: DebugLoc.cpp:25
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:863
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:118
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition: DebugLoc.cpp:45
bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition: DebugLoc.cpp:58
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:43
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:266
LLVMContext & getContext() const
Definition: Metadata.h:923
Debug location.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1165
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1173
MDNode * getScope() const
Definition: DebugLoc.cpp:35
DILocation * getInlinedAt() const
Definition: DebugLoc.cpp:40
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition: DebugLoc.cpp:49
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
DebugLoc()=default
unsigned getCol() const
Definition: DebugLoc.cpp:30
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void dump() const
Definition: DebugLoc.cpp:115
void setImplicitCode(bool ImplicitCode)
Definition: DebugLoc.cpp:65