DebugLoc.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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::replaceInlinedAtSubprogram(
  58. const DebugLoc &RootLoc, DISubprogram &NewSP, LLVMContext &Ctx,
  59. DenseMap<const MDNode *, MDNode *> &Cache) {
  60. SmallVector<DILocation *> LocChain;
  61. DILocation *CachedResult = nullptr;
  62. // Collect the inline chain, stopping if we find a location that has already
  63. // been processed.
  64. for (DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
  65. if (auto It = Cache.find(Loc); It != Cache.end()) {
  66. CachedResult = cast<DILocation>(It->second);
  67. break;
  68. }
  69. LocChain.push_back(Loc);
  70. }
  71. DILocation *UpdatedLoc = CachedResult;
  72. if (!UpdatedLoc) {
  73. // If no cache hits, then back() is the end of the inline chain, that is,
  74. // the DILocation whose scope ends in the Subprogram to be replaced.
  75. DILocation *LocToUpdate = LocChain.pop_back_val();
  76. DIScope *NewScope = DILocalScope::cloneScopeForSubprogram(
  77. *LocToUpdate->getScope(), NewSP, Ctx, Cache);
  78. UpdatedLoc = DILocation::get(Ctx, LocToUpdate->getLine(),
  79. LocToUpdate->getColumn(), NewScope);
  80. Cache[LocToUpdate] = UpdatedLoc;
  81. }
  82. // Recreate the location chain, bottom-up, starting at the new scope (or a
  83. // cached result).
  84. for (const DILocation *LocToUpdate : reverse(LocChain)) {
  85. UpdatedLoc =
  86. DILocation::get(Ctx, LocToUpdate->getLine(), LocToUpdate->getColumn(),
  87. LocToUpdate->getScope(), UpdatedLoc);
  88. Cache[LocToUpdate] = UpdatedLoc;
  89. }
  90. return UpdatedLoc;
  91. }
  92. DebugLoc DebugLoc::appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt,
  93. LLVMContext &Ctx,
  94. DenseMap<const MDNode *, MDNode *> &Cache) {
  95. SmallVector<DILocation *, 3> InlinedAtLocations;
  96. DILocation *Last = InlinedAt;
  97. DILocation *CurInlinedAt = DL;
  98. // Gather all the inlined-at nodes.
  99. while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
  100. // Skip any we've already built nodes for.
  101. if (auto *Found = Cache[IA]) {
  102. Last = cast<DILocation>(Found);
  103. break;
  104. }
  105. InlinedAtLocations.push_back(IA);
  106. CurInlinedAt = IA;
  107. }
  108. // Starting from the top, rebuild the nodes to point to the new inlined-at
  109. // location (then rebuilding the rest of the chain behind it) and update the
  110. // map of already-constructed inlined-at nodes.
  111. for (const DILocation *MD : reverse(InlinedAtLocations))
  112. Cache[MD] = Last = DILocation::getDistinct(
  113. Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
  114. return Last;
  115. }
  116. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  117. LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
  118. #endif
  119. void DebugLoc::print(raw_ostream &OS) const {
  120. if (!Loc)
  121. return;
  122. // Print source line info.
  123. auto *Scope = cast<DIScope>(getScope());
  124. OS << Scope->getFilename();
  125. OS << ':' << getLine();
  126. if (getCol() != 0)
  127. OS << ':' << getCol();
  128. if (DebugLoc InlinedAtDL = getInlinedAt()) {
  129. OS << " @[ ";
  130. InlinedAtDL.print(OS);
  131. OS << " ]";
  132. }
  133. }