blob: 515e0a2b7161915737386ccbbb69fcb175b88632 [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//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000019DebugLoc::DebugLoc(MDLocation *L) : Loc(L) {}
20DebugLoc::DebugLoc(MDNode *L) : Loc(L) {}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000021
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000022MDLocation *DebugLoc::get() const {
23 return cast_or_null<MDLocation>(Loc.get());
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000024}
25
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000026unsigned DebugLoc::getLine() const {
27 assert(get() && "Expected valid DebugLoc");
28 return get()->getLine();
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000029}
30
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000031unsigned DebugLoc::getCol() const {
32 assert(get() && "Expected valid DebugLoc");
33 return get()->getColumn();
34}
35
36MDNode *DebugLoc::getScope() const {
37 assert(get() && "Expected valid DebugLoc");
38 return get()->getScope();
39}
40
41MDLocation *DebugLoc::getInlinedAt() const {
42 assert(get() && "Expected valid DebugLoc");
43 return get()->getInlinedAt();
44}
45
46MDNode *DebugLoc::getInlinedAtScope() const {
Duncan P. N. Exon Smith8f7bc792015-03-30 17:41:24 +000047 return cast<MDLocation>(Loc)->getInlinedAtScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000048}
49
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000050DebugLoc DebugLoc::getFnDebugLoc() const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000051 // FIXME: Add a method on \a MDLocation that does this work.
52 const MDNode *Scope = getInlinedAtScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000053 DISubprogram SP = getDISubprogram(Scope);
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000054 if (SP.isSubprogram())
55 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000056
57 return DebugLoc();
58}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000059
Chris Lattner593916d2010-04-02 20:21:22 +000060DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
61 MDNode *Scope, MDNode *InlinedAt) {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000062 // If no scope is available, this is an unknown location.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000063 if (!Scope)
64 return DebugLoc();
Craig Topperc6207612014-04-09 06:08:46 +000065
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000066 return MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt);
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
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000080 if (!Loc)
81 return;
82
83 dbgs() << getLine();
84 if (getCol() != 0)
85 dbgs() << ',' << getCol();
86 if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
87 dbgs() << " @ ";
88 InlinedAtDL.dump();
89 } else
90 dbgs() << "\n";
Devang Patel4db38442011-07-14 21:50:04 +000091#endif
92}
93
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000094void DebugLoc::print(raw_ostream &OS) const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000095 if (!Loc)
96 return;
97
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
110 if (DebugLoc InlinedAtDL = getInlinedAt()) {
111 OS << " @[ ";
112 InlinedAtDL.print(OS);
113 OS << " ]";
Zinovy Nisda925c02014-05-07 09:51:22 +0000114 }
115}
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +0000116
117// FIXME: Remove this old API once callers have been updated.
118MDNode *DebugLoc::getInlinedAt(const LLVMContext &) const {
119 return getInlinedAt();
120}
121void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
122 Scope = getScope();
123 IA = getInlinedAt();
124}