LLVM  8.0.0svn
DebugLoc.cpp
Go to the documentation of this file.
1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/IR/DebugLoc.h"
11 #include "LLVMContextImpl.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/IR/DebugInfo.h"
14 using namespace llvm;
15 
16 //===----------------------------------------------------------------------===//
17 // DebugLoc Implementation
18 //===----------------------------------------------------------------------===//
19 DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
20 DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
21 
23  return cast_or_null<DILocation>(Loc.get());
24 }
25 
26 unsigned DebugLoc::getLine() const {
27  assert(get() && "Expected valid DebugLoc");
28  return get()->getLine();
29 }
30 
31 unsigned DebugLoc::getCol() const {
32  assert(get() && "Expected valid DebugLoc");
33  return get()->getColumn();
34 }
35 
37  assert(get() && "Expected valid DebugLoc");
38  return get()->getScope();
39 }
40 
42  assert(get() && "Expected valid DebugLoc");
43  return get()->getInlinedAt();
44 }
45 
47  return cast<DILocation>(Loc)->getInlinedAtScope();
48 }
49 
51  // FIXME: Add a method on \a DILocation that does this work.
52  const MDNode *Scope = getInlinedAtScope();
53  if (auto *SP = getDISubprogram(Scope))
54  return DebugLoc::get(SP->getScopeLine(), 0, SP);
55 
56  return DebugLoc();
57 }
58 
60  if (DILocation *Loc = get()) {
61  return Loc->isImplicitCode();
62  }
63  return true;
64 }
65 
66 void DebugLoc::setImplicitCode(bool ImplicitCode) {
67  if (DILocation *Loc = get()) {
68  Loc->setImplicitCode(ImplicitCode);
69  }
70 }
71 
72 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
73  const MDNode *InlinedAt, bool ImplicitCode) {
74  // If no scope is available, this is an unknown location.
75  if (!Scope)
76  return DebugLoc();
77 
78  return DILocation::get(Scope->getContext(), Line, Col,
79  const_cast<MDNode *>(Scope),
80  const_cast<MDNode *>(InlinedAt), ImplicitCode);
81 }
82 
84  LLVMContext &Ctx,
86  bool ReplaceLast) {
87  SmallVector<DILocation *, 3> InlinedAtLocations;
88  DILocation *Last = InlinedAt;
89  DILocation *CurInlinedAt = DL;
90 
91  // Gather all the inlined-at nodes.
92  while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
93  // Skip any we've already built nodes for.
94  if (auto *Found = Cache[IA]) {
95  Last = cast<DILocation>(Found);
96  break;
97  }
98 
99  if (ReplaceLast && !IA->getInlinedAt())
100  break;
101  InlinedAtLocations.push_back(IA);
102  CurInlinedAt = IA;
103  }
104 
105  // Starting from the top, rebuild the nodes to point to the new inlined-at
106  // location (then rebuilding the rest of the chain behind it) and update the
107  // map of already-constructed inlined-at nodes.
108  for (const DILocation *MD : reverse(InlinedAtLocations))
109  Cache[MD] = Last = DILocation::getDistinct(
110  Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
111 
112  return Last;
113 }
114 
115 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
117 #endif
118 
119 void DebugLoc::print(raw_ostream &OS) const {
120  if (!Loc)
121  return;
122 
123  // Print source line info.
124  auto *Scope = cast<DIScope>(getScope());
125  OS << Scope->getFilename();
126  OS << ':' << getLine();
127  if (getCol() != 0)
128  OS << ':' << getCol();
129 
130  if (DebugLoc InlinedAtDL = getInlinedAt()) {
131  OS << " @[ ";
132  InlinedAtDL.print(OS);
133  OS << " ]";
134  }
135 }
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:22
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:83
unsigned getLine() const
Definition: DebugLoc.cpp:26
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:864
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:119
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition: DebugLoc.cpp:46
bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition: DebugLoc.cpp:59
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:44
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:251
LLVMContext & getContext() const
Definition: Metadata.h:924
Debug location.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1166
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
MDNode * getScope() const
Definition: DebugLoc.cpp:36
DILocation * getInlinedAt() const
Definition: DebugLoc.cpp:41
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition: DebugLoc.cpp:50
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
DebugLoc()=default
unsigned getCol() const
Definition: DebugLoc.cpp:31
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:46
void dump() const
Definition: DebugLoc.cpp:116
void setImplicitCode(bool ImplicitCode)
Definition: DebugLoc.cpp:66