Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 1 | //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 9 | #include "llvm/IR/DebugLoc.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 10 | #include "LLVMContextImpl.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 11 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DebugInfo.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 13 | using namespace llvm; |
| 14 | |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | // DebugLoc Implementation |
| 17 | //===----------------------------------------------------------------------===// |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 18 | DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {} |
Duncan P. N. Exon Smith | 5c8f1dc | 2015-04-16 16:56:29 +0000 | [diff] [blame] | 19 | DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {} |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 20 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 21 | DILocation *DebugLoc::get() const { |
| 22 | return cast_or_null<DILocation>(Loc.get()); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 25 | unsigned DebugLoc::getLine() const { |
| 26 | assert(get() && "Expected valid DebugLoc"); |
| 27 | return get()->getLine(); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 30 | unsigned DebugLoc::getCol() const { |
| 31 | assert(get() && "Expected valid DebugLoc"); |
| 32 | return get()->getColumn(); |
| 33 | } |
| 34 | |
| 35 | MDNode *DebugLoc::getScope() const { |
| 36 | assert(get() && "Expected valid DebugLoc"); |
| 37 | return get()->getScope(); |
| 38 | } |
| 39 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 40 | DILocation *DebugLoc::getInlinedAt() const { |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 41 | assert(get() && "Expected valid DebugLoc"); |
| 42 | return get()->getInlinedAt(); |
| 43 | } |
| 44 | |
| 45 | MDNode *DebugLoc::getInlinedAtScope() const { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 46 | return cast<DILocation>(Loc)->getInlinedAtScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 49 | DebugLoc DebugLoc::getFnDebugLoc() const { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 50 | // FIXME: Add a method on \a DILocation that does this work. |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 51 | const MDNode *Scope = getInlinedAtScope(); |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 52 | if (auto *SP = getDISubprogram(Scope)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 53 | return DebugLoc::get(SP->getScopeLine(), 0, SP); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 54 | |
| 55 | return DebugLoc(); |
| 56 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 57 | |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 58 | bool DebugLoc::isImplicitCode() const { |
| 59 | if (DILocation *Loc = get()) { |
| 60 | return Loc->isImplicitCode(); |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void DebugLoc::setImplicitCode(bool ImplicitCode) { |
| 66 | if (DILocation *Loc = get()) { |
| 67 | Loc->setImplicitCode(ImplicitCode); |
| 68 | } |
| 69 | } |
| 70 | |
Duncan P. N. Exon Smith | 5c8f1dc | 2015-04-16 16:56:29 +0000 | [diff] [blame] | 71 | DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope, |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 72 | const MDNode *InlinedAt, bool ImplicitCode) { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 73 | // If no scope is available, this is an unknown location. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 74 | if (!Scope) |
| 75 | return DebugLoc(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 76 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 77 | return DILocation::get(Scope->getContext(), Line, Col, |
Duncan P. N. Exon Smith | 5c8f1dc | 2015-04-16 16:56:29 +0000 | [diff] [blame] | 78 | const_cast<MDNode *>(Scope), |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 79 | const_cast<MDNode *>(InlinedAt), ImplicitCode); |
Chris Lattner | 56e4b4a | 2010-04-01 03:55:42 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 81 | |
Adrian Prantl | c10d0e5 | 2017-05-09 19:47:37 +0000 | [diff] [blame] | 82 | DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt, |
| 83 | LLVMContext &Ctx, |
| 84 | DenseMap<const MDNode *, MDNode *> &Cache, |
| 85 | bool ReplaceLast) { |
| 86 | SmallVector<DILocation *, 3> InlinedAtLocations; |
| 87 | DILocation *Last = InlinedAt; |
| 88 | DILocation *CurInlinedAt = DL; |
| 89 | |
| 90 | // Gather all the inlined-at nodes. |
| 91 | while (DILocation *IA = CurInlinedAt->getInlinedAt()) { |
| 92 | // Skip any we've already built nodes for. |
| 93 | if (auto *Found = Cache[IA]) { |
| 94 | Last = cast<DILocation>(Found); |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if (ReplaceLast && !IA->getInlinedAt()) |
| 99 | break; |
| 100 | InlinedAtLocations.push_back(IA); |
| 101 | CurInlinedAt = IA; |
| 102 | } |
| 103 | |
| 104 | // Starting from the top, rebuild the nodes to point to the new inlined-at |
| 105 | // location (then rebuilding the rest of the chain behind it) and update the |
| 106 | // map of already-constructed inlined-at nodes. |
| 107 | for (const DILocation *MD : reverse(InlinedAtLocations)) |
| 108 | Cache[MD] = Last = DILocation::getDistinct( |
| 109 | Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last); |
| 110 | |
| 111 | return Last; |
| 112 | } |
| 113 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 114 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Sean Silva | 673f4b5 | 2018-03-15 22:51:55 +0000 | [diff] [blame] | 115 | LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 116 | #endif |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 117 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 118 | void DebugLoc::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 119 | if (!Loc) |
| 120 | return; |
| 121 | |
| 122 | // Print source line info. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 123 | auto *Scope = cast<DIScope>(getScope()); |
Duncan P. N. Exon Smith | b273d06 | 2015-04-16 01:37:00 +0000 | [diff] [blame] | 124 | OS << Scope->getFilename(); |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 125 | OS << ':' << getLine(); |
| 126 | if (getCol() != 0) |
| 127 | OS << ':' << getCol(); |
| 128 | |
| 129 | if (DebugLoc InlinedAtDL = getInlinedAt()) { |
| 130 | OS << " @[ "; |
| 131 | InlinedAtDL.print(OS); |
| 132 | OS << " ]"; |
Zinovy Nis | da925c0 | 2014-05-07 09:51:22 +0000 | [diff] [blame] | 133 | } |
| 134 | } |