1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/DebugLoc.h"
11 #include "LLVMContextImpl.h"
12 #include "llvm/IR/DebugInfo.h"
15 //===----------------------------------------------------------------------===//
16 // DebugLoc Implementation
17 //===----------------------------------------------------------------------===//
18 DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
19 DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
21 DILocation *DebugLoc::get() const {
22 return cast_or_null<DILocation>(Loc.get());
25 unsigned DebugLoc::getLine() const {
26 assert(get() && "Expected valid DebugLoc");
27 return get()->getLine();
30 unsigned DebugLoc::getCol() const {
31 assert(get() && "Expected valid DebugLoc");
32 return get()->getColumn();
35 MDNode *DebugLoc::getScope() const {
36 assert(get() && "Expected valid DebugLoc");
37 return get()->getScope();
40 DILocation *DebugLoc::getInlinedAt() const {
41 assert(get() && "Expected valid DebugLoc");
42 return get()->getInlinedAt();
45 MDNode *DebugLoc::getInlinedAtScope() const {
46 return cast<DILocation>(Loc)->getInlinedAtScope();
49 DebugLoc DebugLoc::getFnDebugLoc() const {
50 // FIXME: Add a method on \a DILocation that does this work.
51 const MDNode *Scope = getInlinedAtScope();
52 if (auto *SP = getDISubprogram(Scope))
53 return DebugLoc::get(SP->getScopeLine(), 0, SP);
58 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
59 const MDNode *InlinedAt) {
60 // If no scope is available, this is an unknown location.
64 return DILocation::get(Scope->getContext(), Line, Col,
65 const_cast<MDNode *>(Scope),
66 const_cast<MDNode *>(InlinedAt));
69 DebugLoc DebugLoc::appendInlinedAt(DebugLoc DL, DILocation *InlinedAt,
71 DenseMap<const MDNode *, MDNode *> &Cache,
73 SmallVector<DILocation *, 3> InlinedAtLocations;
74 DILocation *Last = InlinedAt;
75 DILocation *CurInlinedAt = DL;
77 // Gather all the inlined-at nodes.
78 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
79 // Skip any we've already built nodes for.
80 if (auto *Found = Cache[IA]) {
81 Last = cast<DILocation>(Found);
85 if (ReplaceLast && !IA->getInlinedAt())
87 InlinedAtLocations.push_back(IA);
91 // Starting from the top, rebuild the nodes to point to the new inlined-at
92 // location (then rebuilding the rest of the chain behind it) and update the
93 // map of already-constructed inlined-at nodes.
94 for (const DILocation *MD : reverse(InlinedAtLocations))
95 Cache[MD] = Last = DILocation::getDistinct(
96 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
101 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
102 LLVM_DUMP_METHOD void DebugLoc::dump() const {
108 dbgs() << ',' << getCol();
109 if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
117 void DebugLoc::print(raw_ostream &OS) const {
121 // Print source line info.
122 auto *Scope = cast<DIScope>(getScope());
123 OS << Scope->getFilename();
124 OS << ':' << getLine();
126 OS << ':' << getCol();
128 if (DebugLoc InlinedAtDL = getInlinedAt()) {
130 InlinedAtDL.print(OS);