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 20 unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); } 21 unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); } 22 23 MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); } 24 25 MDNode *DebugLoc::getInlinedAt() const { 26 return DILocation(Loc).getOrigLocation(); 27 } 28 29 /// Return both the Scope and the InlinedAt values. 30 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const { 31 Scope = getScope(); 32 IA = getInlinedAt(); 33 } 34 35 MDNode *DebugLoc::getScopeNode() const { 36 if (MDNode *InlinedAt = getInlinedAt()) 37 return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(); 38 return getScope(); 39 } 40 41 DebugLoc DebugLoc::getFnDebugLoc() const { 42 const MDNode *Scope = getScopeNode(); 43 DISubprogram SP = getDISubprogram(Scope); 44 if (SP.isSubprogram()) 45 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); 46 47 return DebugLoc(); 48 } 49 50 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, 51 MDNode *Scope, MDNode *InlinedAt) { 52 // If no scope is available, this is an unknown location. 53 if (!Scope) 54 return DebugLoc(); 55 56 return getFromDILocation( 57 MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt)); 58 } 59 60 /// getAsMDNode - This method converts the compressed DebugLoc node into a 61 /// DILocation-compatible MDNode. 62 MDNode *DebugLoc::getAsMDNode() const { return Loc; } 63 64 /// getFromDILocation - Translate the DILocation quad into a DebugLoc. 65 DebugLoc DebugLoc::getFromDILocation(MDNode *N) { 66 DebugLoc Loc; 67 Loc.Loc.reset(N); 68 return Loc; 69 } 70 71 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc. 72 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) { 73 DILexicalBlock LexBlock(N); 74 MDNode *Scope = LexBlock.getContext(); 75 if (!Scope) return DebugLoc(); 76 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope, 77 nullptr); 78 } 79 80 void DebugLoc::dump() const { 81 #ifndef NDEBUG 82 if (!isUnknown()) { 83 dbgs() << getLine(); 84 if (getCol() != 0) 85 dbgs() << ',' << getCol(); 86 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); 87 if (!InlinedAtDL.isUnknown()) { 88 dbgs() << " @ "; 89 InlinedAtDL.dump(); 90 } else 91 dbgs() << "\n"; 92 } 93 #endif 94 } 95 96 void DebugLoc::print(raw_ostream &OS) const { 97 if (!isUnknown()) { 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 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); 110 if (!InlinedAtDL.isUnknown()) { 111 OS << " @[ "; 112 InlinedAtDL.print(OS); 113 OS << " ]"; 114 } 115 } 116 } 117