515e0a2b7161915737386ccbbb69fcb175b88632
[oota-llvm.git] / lib / IR / DebugLoc.cpp
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(MDLocation *L) : Loc(L) {}
20 DebugLoc::DebugLoc(MDNode *L) : Loc(L) {}
21
22 MDLocation *DebugLoc::get() const {
23   return cast_or_null<MDLocation>(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
36 MDNode *DebugLoc::getScope() const {
37   assert(get() && "Expected valid DebugLoc");
38   return get()->getScope();
39 }
40
41 MDLocation *DebugLoc::getInlinedAt() const {
42   assert(get() && "Expected valid DebugLoc");
43   return get()->getInlinedAt();
44 }
45
46 MDNode *DebugLoc::getInlinedAtScope() const {
47   return cast<MDLocation>(Loc)->getInlinedAtScope();
48 }
49
50 DebugLoc DebugLoc::getFnDebugLoc() const {
51   // FIXME: Add a method on \a MDLocation that does this work.
52   const MDNode *Scope = getInlinedAtScope();
53   DISubprogram SP = getDISubprogram(Scope);
54   if (SP.isSubprogram())
55     return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
56
57   return DebugLoc();
58 }
59
60 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
61                        MDNode *Scope, MDNode *InlinedAt) {
62   // If no scope is available, this is an unknown location.
63   if (!Scope)
64     return DebugLoc();
65
66   return MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt);
67 }
68
69 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
70 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
71   DILexicalBlock LexBlock(N);
72   MDNode *Scope = LexBlock.getContext();
73   if (!Scope) return DebugLoc();
74   return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
75              nullptr);
76 }
77
78 void DebugLoc::dump() const {
79 #ifndef NDEBUG
80   if (!Loc)
81     return;
82
83   dbgs() << getLine();
84   if (getCol() != 0)
85     dbgs() << ',' << getCol();
86   if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
87     dbgs() << " @ ";
88     InlinedAtDL.dump();
89   } else
90     dbgs() << "\n";
91 #endif
92 }
93
94 void DebugLoc::print(raw_ostream &OS) const {
95   if (!Loc)
96     return;
97
98   // Print source line info.
99   DIScope Scope(getScope());
100   assert((!Scope || Scope.isScope()) &&
101          "Scope of a DebugLoc should be null or a DIScope.");
102   if (Scope)
103     OS << Scope.getFilename();
104   else
105     OS << "<unknown>";
106   OS << ':' << getLine();
107   if (getCol() != 0)
108     OS << ':' << getCol();
109
110   if (DebugLoc InlinedAtDL = getInlinedAt()) {
111     OS << " @[ ";
112     InlinedAtDL.print(OS);
113     OS << " ]";
114   }
115 }
116
117 // FIXME: Remove this old API once callers have been updated.
118 MDNode *DebugLoc::getInlinedAt(const LLVMContext &) const {
119   return getInlinedAt();
120 }
121 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
122   Scope = getScope();
123   IA = getInlinedAt();
124 }