blob: 88f2dbc5410cf5cf88c4263b81e3d25095b630c7 [file] [log] [blame]
Chris Lattner8cb2aeb2010-04-01 00:37:44 +00001//===-- 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
Chandler Carruth92051402014-03-05 10:30:38 +000010#include "llvm/IR/DebugLoc.h"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000011#include "LLVMContextImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/DenseMapInfo.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000013#include "llvm/IR/DebugInfo.h"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000014using namespace llvm;
15
16//===----------------------------------------------------------------------===//
17// DebugLoc Implementation
18//===----------------------------------------------------------------------===//
19
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000020unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); }
21unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); }
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000022
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000023MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); }
24
25MDNode *DebugLoc::getInlinedAt() const {
26 return DILocation(Loc).getOrigLocation();
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000027}
28
29/// Return both the Scope and the InlinedAt values.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000030void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
31 Scope = getScope();
32 IA = getInlinedAt();
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000033}
34
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000035MDNode *DebugLoc::getScopeNode() const {
36 if (MDNode *InlinedAt = getInlinedAt())
37 return DebugLoc::getFromDILocation(InlinedAt).getScopeNode();
38 return getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000039}
40
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000041DebugLoc DebugLoc::getFnDebugLoc() const {
42 const MDNode *Scope = getScopeNode();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000043 DISubprogram SP = getDISubprogram(Scope);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000044 if (SP.isSubprogram())
45 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000046
47 return DebugLoc();
48}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000049
Chris Lattner593916d2010-04-02 20:21:22 +000050DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
51 MDNode *Scope, MDNode *InlinedAt) {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000052 // If no scope is available, this is an unknown location.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000053 if (!Scope)
54 return DebugLoc();
Craig Topperc6207612014-04-09 06:08:46 +000055
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000056 // Saturate line and col to "unknown".
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000057 // FIXME: Allow 16-bits for columns.
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000058 if (Col > 255) Col = 0;
59 if (Line >= (1 << 24)) Line = 0;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000060
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000061 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)),
65 Scope, InlinedAt};
66 return getFromDILocation(MDNode::get(Context, Elts));
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000067}
68
69/// getAsMDNode - This method converts the compressed DebugLoc node into a
Alon Mishne0394c1e2014-02-05 14:23:18 +000070/// DILocation-compatible MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000071MDNode *DebugLoc::getAsMDNode() const { return Loc; }
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000072
Chris Lattner593916d2010-04-02 20:21:22 +000073/// getFromDILocation - Translate the DILocation quad into a DebugLoc.
74DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000075 DebugLoc Loc;
76 Loc.Loc.reset(N);
77 return Loc;
Chris Lattner56e4b4a2010-04-01 03:55:42 +000078}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000079
Devang Patel07d61ed2011-07-14 01:14:57 +000080/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
81DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
Bill Wendling786de352012-07-07 00:52:35 +000082 DILexicalBlock LexBlock(N);
83 MDNode *Scope = LexBlock.getContext();
Craig Topperc6207612014-04-09 06:08:46 +000084 if (!Scope) return DebugLoc();
85 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
86 nullptr);
Devang Patel07d61ed2011-07-14 01:14:57 +000087}
88
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000089void DebugLoc::dump() const {
Devang Patel4db38442011-07-14 21:50:04 +000090#ifndef NDEBUG
91 if (!isUnknown()) {
92 dbgs() << getLine();
93 if (getCol() != 0)
94 dbgs() << ',' << getCol();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000095 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
Devang Patel4db38442011-07-14 21:50:04 +000096 if (!InlinedAtDL.isUnknown()) {
97 dbgs() << " @ ";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000098 InlinedAtDL.dump();
Devang Patel4db38442011-07-14 21:50:04 +000099 } else
100 dbgs() << "\n";
101 }
102#endif
103}
104
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000105void DebugLoc::print(raw_ostream &OS) const {
Zinovy Nisda925c02014-05-07 09:51:22 +0000106 if (!isUnknown()) {
107 // Print source line info.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000108 DIScope Scope(getScope());
Zinovy Nisda925c02014-05-07 09:51:22 +0000109 assert((!Scope || Scope.isScope()) &&
110 "Scope of a DebugLoc should be null or a DIScope.");
111 if (Scope)
112 OS << Scope.getFilename();
113 else
114 OS << "<unknown>";
115 OS << ':' << getLine();
116 if (getCol() != 0)
117 OS << ':' << getCol();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000118 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
Zinovy Nisda925c02014-05-07 09:51:22 +0000119 if (!InlinedAtDL.isUnknown()) {
120 OS << " @[ ";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000121 InlinedAtDL.print(OS);
Zinovy Nisda925c02014-05-07 09:51:22 +0000122 OS << " ]";
123 }
124 }
125}