95f6d67c542e80bb49e323f9cdd4cbe3cabeb156
[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 void DebugLoc::dump() const {
70 #ifndef NDEBUG
71   if (!Loc)
72     return;
73
74   dbgs() << getLine();
75   if (getCol() != 0)
76     dbgs() << ',' << getCol();
77   if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
78     dbgs() << " @ ";
79     InlinedAtDL.dump();
80   } else
81     dbgs() << "\n";
82 #endif
83 }
84
85 void DebugLoc::print(raw_ostream &OS) const {
86   if (!Loc)
87     return;
88
89   // Print source line info.
90   DIScope Scope(getScope());
91   assert((!Scope || Scope.isScope()) &&
92          "Scope of a DebugLoc should be null or a DIScope.");
93   if (Scope)
94     OS << Scope.getFilename();
95   else
96     OS << "<unknown>";
97   OS << ':' << getLine();
98   if (getCol() != 0)
99     OS << ':' << getCol();
100
101   if (DebugLoc InlinedAtDL = getInlinedAt()) {
102     OS << " @[ ";
103     InlinedAtDL.print(OS);
104     OS << " ]";
105   }
106 }