blob: 6297395b4c00920ac563d5227552e37894f56de2 [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 Carruth9a4c9e52014-03-06 00:46:21 +000012#include "llvm/IR/DebugInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "llvm/IR/IntrinsicInst.h"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000014using namespace llvm;
15
16//===----------------------------------------------------------------------===//
17// DebugLoc Implementation
18//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000019DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
Duncan P. N. Exon Smith5c8f1dc2015-04-16 16:56:29 +000020DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000021
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000022DILocation *DebugLoc::get() const {
23 return cast_or_null<DILocation>(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
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000041DILocation *DebugLoc::getInlinedAt() const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000042 assert(get() && "Expected valid DebugLoc");
43 return get()->getInlinedAt();
44}
45
46MDNode *DebugLoc::getInlinedAtScope() const {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000047 return cast<DILocation>(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 Smitha9308c42015-04-29 16:38:44 +000051 // FIXME: Add a method on \a DILocation that does this work.
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000052 const MDNode *Scope = getInlinedAtScope();
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +000053 if (auto *SP = getDISubprogram(Scope))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +000054 return DebugLoc::get(SP->getScopeLine(), 0, SP);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000055
56 return DebugLoc();
57}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000058
Duncan P. N. Exon Smith5c8f1dc2015-04-16 16:56:29 +000059DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
60 const MDNode *InlinedAt) {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000061 // If no scope is available, this is an unknown location.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000062 if (!Scope)
63 return DebugLoc();
Craig Topperc6207612014-04-09 06:08:46 +000064
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000065 return DILocation::get(Scope->getContext(), Line, Col,
Duncan P. N. Exon Smith5c8f1dc2015-04-16 16:56:29 +000066 const_cast<MDNode *>(Scope),
67 const_cast<MDNode *>(InlinedAt));
Chris Lattner56e4b4a2010-04-01 03:55:42 +000068}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000069
Adrian Prantlc10d0e52017-05-09 19:47:37 +000070DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt,
71 LLVMContext &Ctx,
72 DenseMap<const MDNode *, MDNode *> &Cache,
73 bool ReplaceLast) {
74 SmallVector<DILocation *, 3> InlinedAtLocations;
75 DILocation *Last = InlinedAt;
76 DILocation *CurInlinedAt = DL;
77
78 // Gather all the inlined-at nodes.
79 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
80 // Skip any we've already built nodes for.
81 if (auto *Found = Cache[IA]) {
82 Last = cast<DILocation>(Found);
83 break;
84 }
85
86 if (ReplaceLast && !IA->getInlinedAt())
87 break;
88 InlinedAtLocations.push_back(IA);
89 CurInlinedAt = IA;
90 }
91
92 // Starting from the top, rebuild the nodes to point to the new inlined-at
93 // location (then rebuilding the rest of the chain behind it) and update the
94 // map of already-constructed inlined-at nodes.
95 for (const DILocation *MD : reverse(InlinedAtLocations))
96 Cache[MD] = Last = DILocation::getDistinct(
97 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
98
99 return Last;
100}
101
Matthias Braun8c209aa2017-01-28 02:02:38 +0000102#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000103LLVM_DUMP_METHOD void DebugLoc::dump() const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +0000104 if (!Loc)
105 return;
106
107 dbgs() << getLine();
108 if (getCol() != 0)
109 dbgs() << ',' << getCol();
110 if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
111 dbgs() << " @ ";
112 InlinedAtDL.dump();
113 } else
114 dbgs() << "\n";
Devang Patel4db38442011-07-14 21:50:04 +0000115}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000116#endif
Devang Patel4db38442011-07-14 21:50:04 +0000117
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000118void DebugLoc::print(raw_ostream &OS) const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +0000119 if (!Loc)
120 return;
121
122 // Print source line info.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000123 auto *Scope = cast<DIScope>(getScope());
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000124 OS << Scope->getFilename();
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +0000125 OS << ':' << getLine();
126 if (getCol() != 0)
127 OS << ':' << getCol();
128
129 if (DebugLoc InlinedAtDL = getInlinedAt()) {
130 OS << " @[ ";
131 InlinedAtDL.print(OS);
132 OS << " ]";
Zinovy Nisda925c02014-05-07 09:51:22 +0000133 }
134}