984ee24883668fd8437c6cf993cdb5a8dc9fd42a
[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   if (DISubprogram 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,
60                        MDNode *Scope, MDNode *InlinedAt) {
61   // If no scope is available, this is an unknown location.
62   if (!Scope)
63     return DebugLoc();
64
65   return MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt);
66 }
67
68 void DebugLoc::dump() const {
69 #ifndef NDEBUG
70   if (!Loc)
71     return;
72
73   dbgs() << getLine();
74   if (getCol() != 0)
75     dbgs() << ',' << getCol();
76   if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
77     dbgs() << " @ ";
78     InlinedAtDL.dump();
79   } else
80     dbgs() << "\n";
81 #endif
82 }
83
84 void DebugLoc::print(raw_ostream &OS) const {
85   if (!Loc)
86     return;
87
88   // Print source line info.
89   DIScope Scope = cast<MDScope>(getScope());
90   OS << Scope.getFilename();
91   OS << ':' << getLine();
92   if (getCol() != 0)
93     OS << ':' << getCol();
94
95   if (DebugLoc InlinedAtDL = getInlinedAt()) {
96     OS << " @[ ";
97     InlinedAtDL.print(OS);
98     OS << " ]";
99   }
100 }