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 //===----------------------------------------------------------------------===//
20 unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); }
21 unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); }
23 MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); }
25 MDNode *DebugLoc::getInlinedAt() const {
26 return DILocation(Loc).getOrigLocation();
29 /// Return both the Scope and the InlinedAt values.
30 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
35 MDNode *DebugLoc::getScopeNode() const {
36 if (MDNode *InlinedAt = getInlinedAt())
37 return DebugLoc::getFromDILocation(InlinedAt).getScopeNode();
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);
50 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
51 MDNode *Scope, MDNode *InlinedAt) {
52 // If no scope is available, this is an unknown location.
56 // Saturate line and col to "unknown".
57 // FIXME: Allow 16-bits for columns.
58 if (Col > 255) Col = 0;
59 if (Line >= (1 << 24)) Line = 0;
61 LLVMContext &Context = Scope->getContext();
62 Type *Int32 = Type::getInt32Ty(Context);
63 Metadata *Elts[] = {ConstantAsMetadata::get(ConstantInt::get(Int32, Line)),
64 ConstantAsMetadata::get(ConstantInt::get(Int32, Col)),
66 return getFromDILocation(MDNode::get(Context, Elts));
69 /// getAsMDNode - This method converts the compressed DebugLoc node into a
70 /// DILocation-compatible MDNode.
71 MDNode *DebugLoc::getAsMDNode() const { return Loc; }
73 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
74 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
80 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
81 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
82 DILexicalBlock LexBlock(N);
83 MDNode *Scope = LexBlock.getContext();
84 if (!Scope) return DebugLoc();
85 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
89 void DebugLoc::dump() const {
94 dbgs() << ',' << getCol();
95 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
96 if (!InlinedAtDL.isUnknown()) {
105 void DebugLoc::print(raw_ostream &OS) const {
107 // Print source line info.
108 DIScope Scope(getScope());
109 assert((!Scope || Scope.isScope()) &&
110 "Scope of a DebugLoc should be null or a DIScope.");
112 OS << Scope.getFilename();
115 OS << ':' << getLine();
117 OS << ':' << getCol();
118 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
119 if (!InlinedAtDL.isUnknown()) {
121 InlinedAtDL.print(OS);