blob: 10ec98ac7e6c8ee259ee8c64694ff5f3bd7824e2 [file] [log] [blame]
Chris Lattnerb2279252010-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 Carruthb8105172014-03-05 10:30:38 +000010#include "llvm/IR/DebugLoc.h"
Chris Lattnerb2279252010-04-01 00:37:44 +000011#include "LLVMContextImpl.h"
Nico Weber0f38c602018-04-30 14:59:11 +000012#include "llvm/Config/llvm-config.h"
Chandler Carruthf4ec8bf2014-03-06 00:46:21 +000013#include "llvm/IR/DebugInfo.h"
Chris Lattnerb2279252010-04-01 00:37:44 +000014using namespace llvm;
15
16//===----------------------------------------------------------------------===//
17// DebugLoc Implementation
18//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000019DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
Duncan P. N. Exon Smithd6629b32015-04-16 16:56:29 +000020DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
Chris Lattnerb2279252010-04-01 00:37:44 +000021
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000022DILocation *DebugLoc::get() const {
23 return cast_or_null<DILocation>(Loc.get());
Chris Lattnerb2279252010-04-01 00:37:44 +000024}
25
Duncan P. N. Exon Smith1c131972015-03-30 18:07:40 +000026unsigned DebugLoc::getLine() const {
27 assert(get() && "Expected valid DebugLoc");
28 return get()->getLine();
Chris Lattnerb2279252010-04-01 00:37:44 +000029}
30
Duncan P. N. Exon Smith1c131972015-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 Smithe56023a2015-04-29 16:38:44 +000041DILocation *DebugLoc::getInlinedAt() const {
Duncan P. N. Exon Smith1c131972015-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 Smithe56023a2015-04-29 16:38:44 +000047 return cast<DILocation>(Loc)->getInlinedAtScope();
Timur Iskhodzhanovab425782014-01-30 01:39:17 +000048}
49
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +000050DebugLoc DebugLoc::getFnDebugLoc() const {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000051 // FIXME: Add a method on \a DILocation that does this work.
Duncan P. N. Exon Smith1c131972015-03-30 18:07:40 +000052 const MDNode *Scope = getInlinedAtScope();
Duncan P. N. Exon Smithd3c29ac2015-04-20 22:10:08 +000053 if (auto *SP = getDISubprogram(Scope))
Duncan P. N. Exon Smith125e3d32015-04-14 03:40:37 +000054 return DebugLoc::get(SP->getScopeLine(), 0, SP);
Timur Iskhodzhanovab425782014-01-30 01:39:17 +000055
56 return DebugLoc();
57}
Chris Lattnerb2279252010-04-01 00:37:44 +000058
Calixte Denizet44db1d12018-09-20 08:53:06 +000059bool DebugLoc::isImplicitCode() const {
60 if (DILocation *Loc = get()) {
61 return Loc->isImplicitCode();
62 }
63 return true;
64}
65
66void DebugLoc::setImplicitCode(bool ImplicitCode) {
67 if (DILocation *Loc = get()) {
68 Loc->setImplicitCode(ImplicitCode);
69 }
70}
71
Duncan P. N. Exon Smithd6629b32015-04-16 16:56:29 +000072DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
Calixte Denizet44db1d12018-09-20 08:53:06 +000073 const MDNode *InlinedAt, bool ImplicitCode) {
Chris Lattnerb2279252010-04-01 00:37:44 +000074 // If no scope is available, this is an unknown location.
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +000075 if (!Scope)
76 return DebugLoc();
Craig Topperec0f0bc2014-04-09 06:08:46 +000077
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000078 return DILocation::get(Scope->getContext(), Line, Col,
Duncan P. N. Exon Smithd6629b32015-04-16 16:56:29 +000079 const_cast<MDNode *>(Scope),
Calixte Denizet44db1d12018-09-20 08:53:06 +000080 const_cast<MDNode *>(InlinedAt), ImplicitCode);
Chris Lattner68c55132010-04-01 03:55:42 +000081}
Chris Lattnerb2279252010-04-01 00:37:44 +000082
Adrian Prantl8b2dee82017-05-09 19:47:37 +000083DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt,
84 LLVMContext &Ctx,
85 DenseMap<const MDNode *, MDNode *> &Cache,
86 bool ReplaceLast) {
87 SmallVector<DILocation *, 3> InlinedAtLocations;
88 DILocation *Last = InlinedAt;
89 DILocation *CurInlinedAt = DL;
90
91 // Gather all the inlined-at nodes.
92 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
93 // Skip any we've already built nodes for.
94 if (auto *Found = Cache[IA]) {
95 Last = cast<DILocation>(Found);
96 break;
97 }
98
99 if (ReplaceLast && !IA->getInlinedAt())
100 break;
101 InlinedAtLocations.push_back(IA);
102 CurInlinedAt = IA;
103 }
104
105 // Starting from the top, rebuild the nodes to point to the new inlined-at
106 // location (then rebuilding the rest of the chain behind it) and update the
107 // map of already-constructed inlined-at nodes.
108 for (const DILocation *MD : reverse(InlinedAtLocations))
109 Cache[MD] = Last = DILocation::getDistinct(
110 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
111
112 return Last;
113}
114
Aaron Ballman1d03d382017-10-15 14:32:27 +0000115#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sean Silvafd345c12018-03-15 22:51:55 +0000116LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
Matthias Braun88d20752017-01-28 02:02:38 +0000117#endif
Devang Patel3dcb4ef2011-07-14 21:50:04 +0000118
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000119void DebugLoc::print(raw_ostream &OS) const {
Duncan P. N. Exon Smith1c131972015-03-30 18:07:40 +0000120 if (!Loc)
121 return;
122
123 // Print source line info.
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000124 auto *Scope = cast<DIScope>(getScope());
Duncan P. N. Exon Smith9c1aa1c2015-04-16 01:37:00 +0000125 OS << Scope->getFilename();
Duncan P. N. Exon Smith1c131972015-03-30 18:07:40 +0000126 OS << ':' << getLine();
127 if (getCol() != 0)
128 OS << ':' << getCol();
129
130 if (DebugLoc InlinedAtDL = getInlinedAt()) {
131 OS << " @[ ";
132 InlinedAtDL.print(OS);
133 OS << " ]";
Zinovy Nis6a48f1c2014-05-07 09:51:22 +0000134 }
135}