aboutsummaryrefslogtreecommitdiffstats
path: root/lib/IR/DebugLoc.cpp
blob: e1bf7951a5882142f8bf062e92a59e4769a0044f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/DebugLoc.h"
#include "LLVMContextImpl.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/IR/DebugInfo.h"
using namespace llvm;

//===----------------------------------------------------------------------===//
// DebugLoc Implementation
//===----------------------------------------------------------------------===//

unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); }
unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); }

MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); }

MDNode *DebugLoc::getInlinedAt() const {
  return DILocation(Loc).getOrigLocation();
}

/// Return both the Scope and the InlinedAt values.
void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
  Scope = getScope();
  IA = getInlinedAt();
}

MDNode *DebugLoc::getScopeNode() const {
  if (MDNode *InlinedAt = getInlinedAt())
    return DebugLoc::getFromDILocation(InlinedAt).getScopeNode();
  return getScope();
}

DebugLoc DebugLoc::getFnDebugLoc() const {
  const MDNode *Scope = getScopeNode();
  DISubprogram SP = getDISubprogram(Scope);
  if (SP.isSubprogram())
    return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);

  return DebugLoc();
}

DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
                       MDNode *Scope, MDNode *InlinedAt) {
  // If no scope is available, this is an unknown location.
  if (!Scope)
    return DebugLoc();

  return getFromDILocation(
      MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt));
}

/// getAsMDNode - This method converts the compressed DebugLoc node into a
/// DILocation-compatible MDNode.
MDNode *DebugLoc::getAsMDNode() const { return Loc; }

/// getFromDILocation - Translate the DILocation quad into a DebugLoc.
DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
  DebugLoc Loc;
  Loc.Loc.reset(N);
  return Loc;
}

/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
  DILexicalBlock LexBlock(N);
  MDNode *Scope = LexBlock.getContext();
  if (!Scope) return DebugLoc();
  return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
             nullptr);
}

void DebugLoc::dump() const {
#ifndef NDEBUG
  if (!isUnknown()) {
    dbgs() << getLine();
    if (getCol() != 0)
      dbgs() << ',' << getCol();
    DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
    if (!InlinedAtDL.isUnknown()) {
      dbgs() << " @ ";
      InlinedAtDL.dump();
    } else
      dbgs() << "\n";
  }
#endif
}

void DebugLoc::print(raw_ostream &OS) const {
  if (!isUnknown()) {
    // Print source line info.
    DIScope Scope(getScope());
    assert((!Scope || Scope.isScope()) &&
           "Scope of a DebugLoc should be null or a DIScope.");
    if (Scope)
      OS << Scope.getFilename();
    else
      OS << "<unknown>";
    OS << ':' << getLine();
    if (getCol() != 0)
      OS << ':' << getCol();
    DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
    if (!InlinedAtDL.isUnknown()) {
      OS << " @[ ";
      InlinedAtDL.print(OS);
      OS << " ]";
    }
  }
}