blob: 12a7181607c73d514f2a12e567a4492e556fd6ff [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"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000013using namespace llvm;
14
15//===----------------------------------------------------------------------===//
16// DebugLoc Implementation
17//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000018DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
Duncan P. N. Exon Smith5c8f1dc2015-04-16 16:56:29 +000019DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000020
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000021DILocation *DebugLoc::get() const {
22 return cast_or_null<DILocation>(Loc.get());
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000023}
24
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000025unsigned DebugLoc::getLine() const {
26 assert(get() && "Expected valid DebugLoc");
27 return get()->getLine();
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000028}
29
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000030unsigned DebugLoc::getCol() const {
31 assert(get() && "Expected valid DebugLoc");
32 return get()->getColumn();
33}
34
35MDNode *DebugLoc::getScope() const {
36 assert(get() && "Expected valid DebugLoc");
37 return get()->getScope();
38}
39
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000040DILocation *DebugLoc::getInlinedAt() const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000041 assert(get() && "Expected valid DebugLoc");
42 return get()->getInlinedAt();
43}
44
45MDNode *DebugLoc::getInlinedAtScope() const {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000046 return cast<DILocation>(Loc)->getInlinedAtScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000047}
48
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000049DebugLoc DebugLoc::getFnDebugLoc() const {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000050 // FIXME: Add a method on \a DILocation that does this work.
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +000051 const MDNode *Scope = getInlinedAtScope();
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +000052 if (auto *SP = getDISubprogram(Scope))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +000053 return DebugLoc::get(SP->getScopeLine(), 0, SP);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000054
55 return DebugLoc();
56}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000057
Duncan P. N. Exon Smith5c8f1dc2015-04-16 16:56:29 +000058DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
59 const MDNode *InlinedAt) {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000060 // If no scope is available, this is an unknown location.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000061 if (!Scope)
62 return DebugLoc();
Craig Topperc6207612014-04-09 06:08:46 +000063
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000064 return DILocation::get(Scope->getContext(), Line, Col,
Duncan P. N. Exon Smith5c8f1dc2015-04-16 16:56:29 +000065 const_cast<MDNode *>(Scope),
66 const_cast<MDNode *>(InlinedAt));
Chris Lattner56e4b4a2010-04-01 03:55:42 +000067}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000068
Adrian Prantlc10d0e52017-05-09 19:47:37 +000069DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt,
70 LLVMContext &Ctx,
71 DenseMap<const MDNode *, MDNode *> &Cache,
72 bool ReplaceLast) {
73 SmallVector<DILocation *, 3> InlinedAtLocations;
74 DILocation *Last = InlinedAt;
75 DILocation *CurInlinedAt = DL;
76
77 // Gather all the inlined-at nodes.
78 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
79 // Skip any we've already built nodes for.
80 if (auto *Found = Cache[IA]) {
81 Last = cast<DILocation>(Found);
82 break;
83 }
84
85 if (ReplaceLast && !IA->getInlinedAt())
86 break;
87 InlinedAtLocations.push_back(IA);
88 CurInlinedAt = IA;
89 }
90
91 // Starting from the top, rebuild the nodes to point to the new inlined-at
92 // location (then rebuilding the rest of the chain behind it) and update the
93 // map of already-constructed inlined-at nodes.
94 for (const DILocation *MD : reverse(InlinedAtLocations))
95 Cache[MD] = Last = DILocation::getDistinct(
96 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
97
98 return Last;
99}
100
Aaron Ballman615eb472017-10-15 14:32:27 +0000101#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sean Silva673f4b52018-03-15 22:51:55 +0000102LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000103#endif
Devang Patel4db38442011-07-14 21:50:04 +0000104
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000105void DebugLoc::print(raw_ostream &OS) const {
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +0000106 if (!Loc)
107 return;
108
109 // Print source line info.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000110 auto *Scope = cast<DIScope>(getScope());
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000111 OS << Scope->getFilename();
Duncan P. N. Exon Smith86b0db42015-03-30 18:07:40 +0000112 OS << ':' << getLine();
113 if (getCol() != 0)
114 OS << ':' << getCol();
115
116 if (DebugLoc InlinedAtDL = getInlinedAt()) {
117 OS << " @[ ";
118 InlinedAtDL.print(OS);
119 OS << " ]";
Zinovy Nisda925c02014-05-07 09:51:22 +0000120 }
121}