LLVM  15.0.0git
DebugLoc.cpp
Go to the documentation of this file.
1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/IR/DebugLoc.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/IR/DebugInfo.h"
12 using namespace llvm;
13 
14 //===----------------------------------------------------------------------===//
15 // DebugLoc Implementation
16 //===----------------------------------------------------------------------===//
17 DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
18 DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
19 
21  return cast_or_null<DILocation>(Loc.get());
22 }
23 
24 unsigned DebugLoc::getLine() const {
25  assert(get() && "Expected valid DebugLoc");
26  return get()->getLine();
27 }
28 
29 unsigned DebugLoc::getCol() const {
30  assert(get() && "Expected valid DebugLoc");
31  return get()->getColumn();
32 }
33 
35  assert(get() && "Expected valid DebugLoc");
36  return get()->getScope();
37 }
38 
40  assert(get() && "Expected valid DebugLoc");
41  return get()->getInlinedAt();
42 }
43 
45  return cast<DILocation>(Loc)->getInlinedAtScope();
46 }
47 
49  // FIXME: Add a method on \a DILocation that does this work.
50  const MDNode *Scope = getInlinedAtScope();
51  if (auto *SP = getDISubprogram(Scope))
52  return DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
53 
54  return DebugLoc();
55 }
56 
58  if (DILocation *Loc = get()) {
59  return Loc->isImplicitCode();
60  }
61  return true;
62 }
63 
64 void DebugLoc::setImplicitCode(bool ImplicitCode) {
65  if (DILocation *Loc = get()) {
66  Loc->setImplicitCode(ImplicitCode);
67  }
68 }
69 
71  LLVMContext &Ctx,
73  SmallVector<DILocation *, 3> InlinedAtLocations;
74  DILocation *Last = InlinedAt;
75  DILocation *CurInlinedAt = DL;
76 
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);
82  break;
83  }
84 
85  InlinedAtLocations.push_back(IA);
86  CurInlinedAt = IA;
87  }
88 
89  // Starting from the top, rebuild the nodes to point to the new inlined-at
90  // location (then rebuilding the rest of the chain behind it) and update the
91  // map of already-constructed inlined-at nodes.
92  for (const DILocation *MD : reverse(InlinedAtLocations))
93  Cache[MD] = Last = DILocation::getDistinct(
94  Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
95 
96  return Last;
97 }
98 
99 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
101 #endif
102 
103 void DebugLoc::print(raw_ostream &OS) const {
104  if (!Loc)
105  return;
106 
107  // Print source line info.
108  auto *Scope = cast<DIScope>(getScope());
109  OS << Scope->getFilename();
110  OS << ':' << getLine();
111  if (getCol() != 0)
112  OS << ':' << getCol();
113 
114  if (DebugLoc InlinedAtDL = getInlinedAt()) {
115  OS << " @[ ";
116  InlinedAtDL.print(OS);
117  OS << " ]";
118  }
119 }
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:494
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::DILocation
Debug location.
Definition: DebugInfoMetadata.h:1557
llvm::getDISubprogram
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:128
llvm::DebugLoc::getInlinedAt
DILocation * getInlinedAt() const
Definition: DebugLoc.cpp:39
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:380
llvm::DebugLoc::DebugLoc
DebugLoc()=default
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1400
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::TypedTrackingMDRef::get
T * get() const
Definition: TrackingMDRef.h:126
llvm::DebugLoc::getCol
unsigned getCol() const
Definition: DebugLoc.cpp:29
llvm::DebugLoc::getFnDebugLoc
DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition: DebugLoc.cpp:48
llvm::DebugLoc::dump
void dump() const
Definition: DebugLoc.cpp:100
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
DebugLoc.h
llvm::DebugLoc::appendInlinedAt
static DebugLoc appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inlined-at chain for this instruction so that the top of the chain now is inlined-...
Definition: DebugLoc.cpp:70
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DenseMap
Definition: DenseMap.h:716
DebugInfo.h
llvm::DebugLoc::get
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:20
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MDNode
Metadata node.
Definition: Metadata.h:944
llvm::DebugLoc::setImplicitCode
void setImplicitCode(bool ImplicitCode)
Definition: DebugLoc.cpp:64
llvm::DebugLoc::isImplicitCode
bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition: DebugLoc.cpp:57
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MDNode::getDistinct
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1408
llvm::DebugLoc::print
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:103
llvm::DebugLoc::getLine
unsigned getLine() const
Definition: DebugLoc.cpp:24
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::DebugLoc::getInlinedAtScope
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition: DebugLoc.cpp:44
llvm::DebugLoc::getScope
MDNode * getScope() const
Definition: DebugLoc.cpp:34