LLVM  3.7.0
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/ADT/DenseMapInfo.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 
59 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
60  const MDNode *InlinedAt) {
61  // If no scope is available, this is an unknown location.
62  if (!Scope)
63  return DebugLoc();
64 
65  return DILocation::get(Scope->getContext(), Line, Col,
66  const_cast<MDNode *>(Scope),
67  const_cast<MDNode *>(InlinedAt));
68 }
69 
70 void DebugLoc::dump() const {
71 #ifndef NDEBUG
72  if (!Loc)
73  return;
74 
75  dbgs() << getLine();
76  if (getCol() != 0)
77  dbgs() << ',' << getCol();
78  if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
79  dbgs() << " @ ";
80  InlinedAtDL.dump();
81  } else
82  dbgs() << "\n";
83 #endif
84 }
85 
86 void DebugLoc::print(raw_ostream &OS) const {
87  if (!Loc)
88  return;
89 
90  // Print source line info.
91  auto *Scope = cast<DIScope>(getScope());
92  OS << Scope->getFilename();
93  OS << ':' << getLine();
94  if (getCol() != 0)
95  OS << ':' << getCol();
96 
97  if (DebugLoc InlinedAtDL = getInlinedAt()) {
98  OS << " @[ ";
99  InlinedAtDL.print(OS);
100  OS << " ]";
101  }
102 }
MDNode * getScope() const
Definition: DebugLoc.cpp:36
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:740
DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition: DebugLoc.cpp:50
void dump() const
Definition: DebugLoc.cpp:70
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:22
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:36
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:86
Debug location.
unsigned getLine() const
Definition: DebugLoc.cpp:26
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition: DebugLoc.cpp:46
DILocation * getInlinedAt() const
Definition: DebugLoc.cpp:41
unsigned getCol() const
Definition: DebugLoc.cpp:31
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1030
LLVMContext & getContext() const
Definition: Metadata.h:799
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38