LLVM  7.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/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 
58 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
59  const MDNode *InlinedAt) {
60  // If no scope is available, this is an unknown location.
61  if (!Scope)
62  return DebugLoc();
63 
64  return DILocation::get(Scope->getContext(), Line, Col,
65  const_cast<MDNode *>(Scope),
66  const_cast<MDNode *>(InlinedAt));
67 }
68 
70  LLVMContext &Ctx,
72  bool ReplaceLast) {
73  SmallVector<DILocation *, 3> InlinedAtLocations;
74  DILocation *Last = InlinedAt;
75  DILocation *CurInlinedAt = DL;
76 
77  // Gather all the inlined-at nodes.
78  while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
79  // Skip any we've already built nodes for.
80  if (auto *Found = Cache[IA]) {
81  Last = cast<DILocation>(Found);
82  break;
83  }
84 
85  if (ReplaceLast && !IA->getInlinedAt())
86  break;
87  InlinedAtLocations.push_back(IA);
88  CurInlinedAt = IA;
89  }
90 
91  // Starting from the top, rebuild the nodes to point to the new inlined-at
92  // location (then rebuilding the rest of the chain behind it) and update the
93  // map of already-constructed inlined-at nodes.
94  for (const DILocation *MD : reverse(InlinedAtLocations))
95  Cache[MD] = Last = DILocation::getDistinct(
96  Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
97 
98  return Last;
99 }
100 
101 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
103 #endif
104 
105 void DebugLoc::print(raw_ostream &OS) const {
106  if (!Loc)
107  return;
108 
109  // Print source line info.
110  auto *Scope = cast<DIScope>(getScope());
111  OS << Scope->getFilename();
112  OS << ':' << getLine();
113  if (getCol() != 0)
114  OS << ':' << getCol();
115 
116  if (DebugLoc InlinedAtDL = getInlinedAt()) {
117  OS << " @[ ";
118  InlinedAtDL.print(OS);
119  OS << " ]";
120  }
121 }
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
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:69
unsigned getLine() const
Definition: DebugLoc.cpp:25
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:862
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:105
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition: DebugLoc.cpp:45
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
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:237
LLVMContext & getContext() const
Definition: Metadata.h:922
Debug location.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
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:1172
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:862
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:44
void dump() const
Definition: DebugLoc.cpp:102