1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/DebugLoc.h"
11 #include "LLVMContextImpl.h"
12 #include "llvm/ADT/DenseMapInfo.h"
13 #include "llvm/IR/DebugInfo.h"
16 //===----------------------------------------------------------------------===//
17 // DebugLoc Implementation
18 //===----------------------------------------------------------------------===//
19 DebugLoc::DebugLoc(MDLocation *L) : Loc(L) {}
20 DebugLoc::DebugLoc(MDNode *L) : Loc(L) {}
22 MDLocation *DebugLoc::get() const {
23 return cast_or_null<MDLocation>(Loc.get());
26 unsigned DebugLoc::getLine() const {
27 assert(get() && "Expected valid DebugLoc");
28 return get()->getLine();
31 unsigned DebugLoc::getCol() const {
32 assert(get() && "Expected valid DebugLoc");
33 return get()->getColumn();
36 MDNode *DebugLoc::getScope() const {
37 assert(get() && "Expected valid DebugLoc");
38 return get()->getScope();
41 MDLocation *DebugLoc::getInlinedAt() const {
42 assert(get() && "Expected valid DebugLoc");
43 return get()->getInlinedAt();
46 MDNode *DebugLoc::getInlinedAtScope() const {
47 return cast<MDLocation>(Loc)->getInlinedAtScope();
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);
60 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
61 MDNode *Scope, MDNode *InlinedAt) {
62 // If no scope is available, this is an unknown location.
66 return MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt);
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,
78 void DebugLoc::dump() const {
85 dbgs() << ',' << getCol();
86 if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
94 void DebugLoc::print(raw_ostream &OS) const {
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.");
103 OS << Scope.getFilename();
106 OS << ':' << getLine();
108 OS << ':' << getCol();
110 if (DebugLoc InlinedAtDL = getInlinedAt()) {
112 InlinedAtDL.print(OS);