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 | //===----------------------------------------------------------------------===// |
| 19 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 20 | unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); } |
| 21 | unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 22 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 23 | MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); } |
| 24 | |
| 25 | MDNode *DebugLoc::getInlinedAt() const { |
| 26 | return DILocation(Loc).getOrigLocation(); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /// Return both the Scope and the InlinedAt values. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 30 | void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const { |
| 31 | Scope = getScope(); |
| 32 | IA = getInlinedAt(); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 35 | MDNode *DebugLoc::getScopeNode() const { |
| 36 | if (MDNode *InlinedAt = getInlinedAt()) |
| 37 | return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(); |
| 38 | return getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 41 | DebugLoc DebugLoc::getFnDebugLoc() const { |
| 42 | const MDNode *Scope = getScopeNode(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 43 | DISubprogram SP = getDISubprogram(Scope); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 44 | if (SP.isSubprogram()) |
| 45 | return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 46 | |
| 47 | return DebugLoc(); |
| 48 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 50 | DebugLoc DebugLoc::get(unsigned Line, unsigned Col, |
| 51 | MDNode *Scope, MDNode *InlinedAt) { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 52 | // 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^] | 53 | if (!Scope) |
| 54 | return DebugLoc(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 56 | // Saturate line and col to "unknown". |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 57 | // FIXME: Allow 16-bits for columns. |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 58 | if (Col > 255) Col = 0; |
| 59 | if (Line >= (1 << 24)) Line = 0; |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 60 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 61 | LLVMContext &Context = Scope->getContext(); |
| 62 | Type *Int32 = Type::getInt32Ty(Context); |
| 63 | Metadata *Elts[] = {ConstantAsMetadata::get(ConstantInt::get(Int32, Line)), |
| 64 | ConstantAsMetadata::get(ConstantInt::get(Int32, Col)), |
| 65 | Scope, InlinedAt}; |
| 66 | return getFromDILocation(MDNode::get(Context, Elts)); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /// getAsMDNode - This method converts the compressed DebugLoc node into a |
Alon Mishne | 0394c1e | 2014-02-05 14:23:18 +0000 | [diff] [blame] | 70 | /// DILocation-compatible MDNode. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 71 | MDNode *DebugLoc::getAsMDNode() const { return Loc; } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 73 | /// getFromDILocation - Translate the DILocation quad into a DebugLoc. |
| 74 | DebugLoc DebugLoc::getFromDILocation(MDNode *N) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 75 | DebugLoc Loc; |
| 76 | Loc.Loc.reset(N); |
| 77 | return Loc; |
Chris Lattner | 56e4b4a | 2010-04-01 03:55:42 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 79 | |
Devang Patel | 07d61ed | 2011-07-14 01:14:57 +0000 | [diff] [blame] | 80 | /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc. |
| 81 | DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) { |
Bill Wendling | 786de35 | 2012-07-07 00:52:35 +0000 | [diff] [blame] | 82 | DILexicalBlock LexBlock(N); |
| 83 | MDNode *Scope = LexBlock.getContext(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 84 | if (!Scope) return DebugLoc(); |
| 85 | return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope, |
| 86 | nullptr); |
Devang Patel | 07d61ed | 2011-07-14 01:14:57 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 89 | void DebugLoc::dump() const { |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 90 | #ifndef NDEBUG |
| 91 | if (!isUnknown()) { |
| 92 | dbgs() << getLine(); |
| 93 | if (getCol() != 0) |
| 94 | dbgs() << ',' << getCol(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 95 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 96 | if (!InlinedAtDL.isUnknown()) { |
| 97 | dbgs() << " @ "; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 98 | InlinedAtDL.dump(); |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 99 | } else |
| 100 | dbgs() << "\n"; |
| 101 | } |
| 102 | #endif |
| 103 | } |
| 104 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 105 | void DebugLoc::print(raw_ostream &OS) const { |
Zinovy Nis | da925c0 | 2014-05-07 09:51:22 +0000 | [diff] [blame] | 106 | if (!isUnknown()) { |
| 107 | // Print source line info. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 108 | DIScope Scope(getScope()); |
Zinovy Nis | da925c0 | 2014-05-07 09:51:22 +0000 | [diff] [blame] | 109 | assert((!Scope || Scope.isScope()) && |
| 110 | "Scope of a DebugLoc should be null or a DIScope."); |
| 111 | if (Scope) |
| 112 | OS << Scope.getFilename(); |
| 113 | else |
| 114 | OS << "<unknown>"; |
| 115 | OS << ':' << getLine(); |
| 116 | if (getCol() != 0) |
| 117 | OS << ':' << getCol(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 118 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); |
Zinovy Nis | da925c0 | 2014-05-07 09:51:22 +0000 | [diff] [blame] | 119 | if (!InlinedAtDL.isUnknown()) { |
| 120 | OS << " @[ "; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame^] | 121 | InlinedAtDL.print(OS); |
Zinovy Nis | da925c0 | 2014-05-07 09:51:22 +0000 | [diff] [blame] | 122 | OS << " ]"; |
| 123 | } |
| 124 | } |
| 125 | } |