DebugLoc.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "llvm/IR/DebugLoc.h"
  9. #include "llvm/Config/llvm-config.h"
  10. #include "llvm/IR/DebugInfo.h"
  11. using namespace llvm;
  12. //===----------------------------------------------------------------------===//
  13. // DebugLoc Implementation
  14. //===----------------------------------------------------------------------===//
  15. DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
  16. DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
  17. DILocation *DebugLoc::get() const {
  18. return cast_or_null<DILocation>(Loc.get());
  19. }
  20. unsigned DebugLoc::getLine() const {
  21. assert(get() && "Expected valid DebugLoc");
  22. return get()->getLine();
  23. }
  24. unsigned DebugLoc::getCol() const {
  25. assert(get() && "Expected valid DebugLoc");
  26. return get()->getColumn();
  27. }
  28. MDNode *DebugLoc::getScope() const {
  29. assert(get() && "Expected valid DebugLoc");
  30. return get()->getScope();
  31. }
  32. DILocation *DebugLoc::getInlinedAt() const {
  33. assert(get() && "Expected valid DebugLoc");
  34. return get()->getInlinedAt();
  35. }
  36. MDNode *DebugLoc::getInlinedAtScope() const {
  37. return cast<DILocation>(Loc)->getInlinedAtScope();
  38. }
  39. DebugLoc DebugLoc::getFnDebugLoc() const {
  40. // FIXME: Add a method on \a DILocation that does this work.
  41. const MDNode *Scope = getInlinedAtScope();
  42. if (auto *SP = getDISubprogram(Scope))
  43. return DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
  44. return DebugLoc();
  45. }
  46. bool DebugLoc::isImplicitCode() const {
  47. if (DILocation *Loc = get()) {
  48. return Loc->isImplicitCode();
  49. }
  50. return true;
  51. }
  52. void DebugLoc::setImplicitCode(bool ImplicitCode) {
  53. if (DILocation *Loc = get()) {
  54. Loc->setImplicitCode(ImplicitCode);
  55. }
  56. }
  57. DebugLoc DebugLoc::appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt,
  58. LLVMContext &Ctx,
  59. DenseMap<const MDNode *, MDNode *> &Cache) {
  60. SmallVector<DILocation *, 3> InlinedAtLocations;
  61. DILocation *Last = InlinedAt;
  62. DILocation *CurInlinedAt = DL;
  63. // Gather all the inlined-at nodes.
  64. while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
  65. // Skip any we've already built nodes for.
  66. if (auto *Found = Cache[IA]) {
  67. Last = cast<DILocation>(Found);
  68. break;
  69. }
  70. InlinedAtLocations.push_back(IA);
  71. CurInlinedAt = IA;
  72. }
  73. // Starting from the top, rebuild the nodes to point to the new inlined-at
  74. // location (then rebuilding the rest of the chain behind it) and update the
  75. // map of already-constructed inlined-at nodes.
  76. for (const DILocation *MD : reverse(InlinedAtLocations))
  77. Cache[MD] = Last = DILocation::getDistinct(
  78. Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
  79. return Last;
  80. }
  81. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  82. LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
  83. #endif
  84. void DebugLoc::print(raw_ostream &OS) const {
  85. if (!Loc)
  86. return;
  87. // Print source line info.
  88. auto *Scope = cast<DIScope>(getScope());
  89. OS << Scope->getFilename();
  90. OS << ':' << getLine();
  91. if (getCol() != 0)
  92. OS << ':' << getCol();
  93. if (DebugLoc InlinedAtDL = getInlinedAt()) {
  94. OS << " @[ ";
  95. InlinedAtDL.print(OS);
  96. OS << " ]";
  97. }
  98. }