Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 1 | //===-- 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 Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 10 | #include "llvm/IR/DebugLoc.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 11 | #include "LLVMContextImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/DenseMapInfo.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 13 | #include "llvm/IR/DebugInfo.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 14 | using namespace llvm; |
| 15 | |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // DebugLoc Implementation |
| 18 | //===----------------------------------------------------------------------===// |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 19 | DebugLoc::DebugLoc(MDLocation *L) : Loc(L) {} |
| 20 | DebugLoc::DebugLoc(MDNode *L) : Loc(L) {} |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 21 | |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 22 | MDLocation *DebugLoc::get() const { |
| 23 | return cast_or_null<MDLocation>(Loc.get()); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 26 | unsigned DebugLoc::getLine() const { |
| 27 | assert(get() && "Expected valid DebugLoc"); |
| 28 | return get()->getLine(); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 31 | unsigned DebugLoc::getCol() const { |
| 32 | assert(get() && "Expected valid DebugLoc"); |
| 33 | return get()->getColumn(); |
| 34 | } |
| 35 | |
| 36 | MDNode *DebugLoc::getScope() const { |
| 37 | assert(get() && "Expected valid DebugLoc"); |
| 38 | return get()->getScope(); |
| 39 | } |
| 40 | |
| 41 | MDLocation *DebugLoc::getInlinedAt() const { |
| 42 | assert(get() && "Expected valid DebugLoc"); |
| 43 | return get()->getInlinedAt(); |
| 44 | } |
| 45 | |
| 46 | MDNode *DebugLoc::getInlinedAtScope() const { |
Duncan P. N. Exon Smith | 8f7bc79 | 2015-03-30 17:41:24 +0000 | [diff] [blame] | 47 | return cast<MDLocation>(Loc)->getInlinedAtScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 50 | DebugLoc DebugLoc::getFnDebugLoc() const { |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 51 | // FIXME: Add a method on \a MDLocation that does this work. |
| 52 | const MDNode *Scope = getInlinedAtScope(); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 53 | if (DISubprogram SP = getDISubprogram(Scope)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame^] | 54 | return DebugLoc::get(SP->getScopeLine(), 0, SP); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 55 | |
| 56 | return DebugLoc(); |
| 57 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 59 | DebugLoc DebugLoc::get(unsigned Line, unsigned Col, |
| 60 | MDNode *Scope, MDNode *InlinedAt) { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 61 | // 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] | 62 | if (!Scope) |
| 63 | return DebugLoc(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 64 | |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 65 | return MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt); |
Chris Lattner | 56e4b4a | 2010-04-01 03:55:42 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 67 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 68 | void DebugLoc::dump() const { |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 69 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 70 | if (!Loc) |
| 71 | return; |
| 72 | |
| 73 | dbgs() << getLine(); |
| 74 | if (getCol() != 0) |
| 75 | dbgs() << ',' << getCol(); |
| 76 | if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) { |
| 77 | dbgs() << " @ "; |
| 78 | InlinedAtDL.dump(); |
| 79 | } else |
| 80 | dbgs() << "\n"; |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 81 | #endif |
| 82 | } |
| 83 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 84 | void DebugLoc::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 85 | if (!Loc) |
| 86 | return; |
| 87 | |
| 88 | // Print source line info. |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 89 | DIScope Scope = cast<MDScope>(getScope()); |
| 90 | OS << Scope.getFilename(); |
Duncan P. N. Exon Smith | 86b0db4 | 2015-03-30 18:07:40 +0000 | [diff] [blame] | 91 | OS << ':' << getLine(); |
| 92 | if (getCol() != 0) |
| 93 | OS << ':' << getCol(); |
| 94 | |
| 95 | if (DebugLoc InlinedAtDL = getInlinedAt()) { |
| 96 | OS << " @[ "; |
| 97 | InlinedAtDL.print(OS); |
| 98 | OS << " ]"; |
Zinovy Nis | da925c0 | 2014-05-07 09:51:22 +0000 | [diff] [blame] | 99 | } |
| 100 | } |