LLVM  14.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 "LLVMContextImpl.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/IR/DebugInfo.h"
13 using namespace llvm;
14 
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)) {}
20 
22  return cast_or_null<DILocation>(Loc.get());
23 }
24 
25 unsigned DebugLoc::getLine() const {
26  assert(get() && "Expected valid DebugLoc");
27  return get()->getLine();
28 }
29 
30 unsigned DebugLoc::getCol() const {
31  assert(get() && "Expected valid DebugLoc");
32  return get()->getColumn();
33 }
34 
36  assert(get() && "Expected valid DebugLoc");
37  return get()->getScope();
38 }
39 
41  assert(get() && "Expected valid DebugLoc");
42  return get()->getInlinedAt();
43 }
44 
46  return cast<DILocation>(Loc)->getInlinedAtScope();
47 }
48 
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 DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
54 
55  return DebugLoc();
56 }
57 
59  if (DILocation *Loc = get()) {
60  return Loc->isImplicitCode();
61  }
62  return true;
63 }
64 
65 void DebugLoc::setImplicitCode(bool ImplicitCode) {
66  if (DILocation *Loc = get()) {
67  Loc->setImplicitCode(ImplicitCode);
68  }
69 }
70 
72  LLVMContext &Ctx,
74  SmallVector<DILocation *, 3> InlinedAtLocations;
75  DILocation *Last = InlinedAt;
76  DILocation *CurInlinedAt = DL;
77 
78  // Gather all the inlined-at nodes.
79  while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
80  // Skip any we've already built nodes for.
81  if (auto *Found = Cache[IA]) {
82  Last = cast<DILocation>(Found);
83  break;
84  }
85 
86  InlinedAtLocations.push_back(IA);
87  CurInlinedAt = IA;
88  }
89 
90  // Starting from the top, rebuild the nodes to point to the new inlined-at
91  // location (then rebuilding the rest of the chain behind it) and update the
92  // map of already-constructed inlined-at nodes.
93  for (const DILocation *MD : reverse(InlinedAtLocations))
94  Cache[MD] = Last = DILocation::getDistinct(
95  Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
96 
97  return Last;
98 }
99 
100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
102 #endif
103 
104 void DebugLoc::print(raw_ostream &OS) const {
105  if (!Loc)
106  return;
107 
108  // Print source line info.
109  auto *Scope = cast<DIScope>(getScope());
110  OS << Scope->getFilename();
111  OS << ':' << getLine();
112  if (getCol() != 0)
113  OS << ':' << getCol();
114 
115  if (DebugLoc InlinedAtDL = getInlinedAt()) {
116  OS << " @[ ";
117  InlinedAtDL.print(OS);
118  OS << " ]";
119  }
120 }
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:491
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::DILocation
Debug location.
Definition: DebugInfoMetadata.h:1580
llvm::getDISubprogram
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:129
llvm::DebugLoc::getInlinedAt
DILocation * getInlinedAt() const
Definition: DebugLoc.cpp:40
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:331
llvm::DebugLoc::DebugLoc
DebugLoc()=default
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1203
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:30
llvm::DebugLoc::getFnDebugLoc
DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition: DebugLoc.cpp:49
llvm::DebugLoc::dump
void dump() const
Definition: DebugLoc.cpp:101
LLVMContextImpl.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
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:71
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::DenseMap
Definition: DenseMap.h:714
DebugInfo.h
llvm::DebugLoc::get
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MDNode
Metadata node.
Definition: Metadata.h:901
llvm::DebugLoc::setImplicitCode
void setImplicitCode(bool ImplicitCode)
Definition: DebugLoc.cpp:65
llvm::DebugLoc::isImplicitCode
bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition: DebugLoc.cpp:58
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MDNode::getDistinct
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1211
llvm::DebugLoc::print
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:104
llvm::DebugLoc::getLine
unsigned getLine() const
Definition: DebugLoc.cpp:25
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:45
llvm::DebugLoc::getScope
MDNode * getScope() const
Definition: DebugLoc.cpp:35