blob: 6b2a53959300e6c5e752e080789bcf9c7185d0c5 [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 {
Duncan P. N. Exon Smith8f7bc792015-03-30 17:41:24 +000036 return cast<MDLocation>(Loc)->getInlinedAtScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000037}
38
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000039DebugLoc DebugLoc::getFnDebugLoc() const {
40 const MDNode *Scope = getScopeNode();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000041 DISubprogram SP = getDISubprogram(Scope);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000042 if (SP.isSubprogram())
43 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000044
45 return DebugLoc();
46}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000047
Chris Lattner593916d2010-04-02 20:21:22 +000048DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
49 MDNode *Scope, MDNode *InlinedAt) {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000050 // If no scope is available, this is an unknown location.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000051 if (!Scope)
52 return DebugLoc();
Craig Topperc6207612014-04-09 06:08:46 +000053
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +000054 return getFromDILocation(
55 MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt));
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000056}
57
58/// getAsMDNode - This method converts the compressed DebugLoc node into a
Alon Mishne0394c1e2014-02-05 14:23:18 +000059/// DILocation-compatible MDNode.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000060MDNode *DebugLoc::getAsMDNode() const { return Loc; }
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000061
Chris Lattner593916d2010-04-02 20:21:22 +000062/// getFromDILocation - Translate the DILocation quad into a DebugLoc.
63DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000064 DebugLoc Loc;
65 Loc.Loc.reset(N);
66 return Loc;
Chris Lattner56e4b4a2010-04-01 03:55:42 +000067}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000068
Devang Patel07d61ed2011-07-14 01:14:57 +000069/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
70DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
Bill Wendling786de352012-07-07 00:52:35 +000071 DILexicalBlock LexBlock(N);
72 MDNode *Scope = LexBlock.getContext();
Craig Topperc6207612014-04-09 06:08:46 +000073 if (!Scope) return DebugLoc();
74 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
75 nullptr);
Devang Patel07d61ed2011-07-14 01:14:57 +000076}
77
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000078void DebugLoc::dump() const {
Devang Patel4db38442011-07-14 21:50:04 +000079#ifndef NDEBUG
80 if (!isUnknown()) {
81 dbgs() << getLine();
82 if (getCol() != 0)
83 dbgs() << ',' << getCol();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000084 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
Devang Patel4db38442011-07-14 21:50:04 +000085 if (!InlinedAtDL.isUnknown()) {
86 dbgs() << " @ ";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000087 InlinedAtDL.dump();
Devang Patel4db38442011-07-14 21:50:04 +000088 } else
89 dbgs() << "\n";
90 }
91#endif
92}
93
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000094void DebugLoc::print(raw_ostream &OS) const {
Zinovy Nisda925c02014-05-07 09:51:22 +000095 if (!isUnknown()) {
96 // Print source line info.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000097 DIScope Scope(getScope());
Zinovy Nisda925c02014-05-07 09:51:22 +000098 assert((!Scope || Scope.isScope()) &&
99 "Scope of a DebugLoc should be null or a DIScope.");
100 if (Scope)
101 OS << Scope.getFilename();
102 else
103 OS << "<unknown>";
104 OS << ':' << getLine();
105 if (getCol() != 0)
106 OS << ':' << getCol();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000107 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
Zinovy Nisda925c02014-05-07 09:51:22 +0000108 if (!InlinedAtDL.isUnknown()) {
109 OS << " @[ ";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000110 InlinedAtDL.print(OS);
Zinovy Nisda925c02014-05-07 09:51:22 +0000111 OS << " ]";
112 }
113 }
114}