DebugLoc.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/IR/DebugLoc.h"
  10. #include "LLVMContextImpl.h"
  11. #include "llvm/ADT/DenseMapInfo.h"
  12. #include "llvm/IR/DebugInfo.h"
  13. using namespace llvm;
  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. DILocation *DebugLoc::get() const {
  20. return cast_or_null<DILocation>(Loc.get());
  21. }
  22. unsigned DebugLoc::getLine() const {
  23. assert(get() && "Expected valid DebugLoc");
  24. return get()->getLine();
  25. }
  26. unsigned DebugLoc::getCol() const {
  27. assert(get() && "Expected valid DebugLoc");
  28. return get()->getColumn();
  29. }
  30. MDNode *DebugLoc::getScope() const {
  31. assert(get() && "Expected valid DebugLoc");
  32. return get()->getScope();
  33. }
  34. DILocation *DebugLoc::getInlinedAt() const {
  35. assert(get() && "Expected valid DebugLoc");
  36. return get()->getInlinedAt();
  37. }
  38. MDNode *DebugLoc::getInlinedAtScope() const {
  39. return cast<DILocation>(Loc)->getInlinedAtScope();
  40. }
  41. DebugLoc DebugLoc::getFnDebugLoc() const {
  42. // FIXME: Add a method on \a DILocation that does this work.
  43. const MDNode *Scope = getInlinedAtScope();
  44. if (auto *SP = getDISubprogram(Scope))
  45. return DebugLoc::get(SP->getScopeLine(), 0, SP);
  46. return DebugLoc();
  47. }
  48. DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
  49. const MDNode *InlinedAt) {
  50. // If no scope is available, this is an unknown location.
  51. if (!Scope)
  52. return DebugLoc();
  53. return DILocation::get(Scope->getContext(), Line, Col,
  54. const_cast<MDNode *>(Scope),
  55. const_cast<MDNode *>(InlinedAt));
  56. }
  57. void DebugLoc::dump() const {
  58. #ifndef NDEBUG
  59. if (!Loc)
  60. return;
  61. dbgs() << getLine();
  62. if (getCol() != 0)
  63. dbgs() << ',' << getCol();
  64. if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
  65. dbgs() << " @ ";
  66. InlinedAtDL.dump();
  67. } else
  68. dbgs() << "\n";
  69. #endif
  70. }
  71. void DebugLoc::print(raw_ostream &OS) const {
  72. if (!Loc)
  73. return;
  74. // Print source line info.
  75. auto *Scope = cast<DIScope>(getScope());
  76. OS << Scope->getFilename();
  77. OS << ':' << getLine();
  78. if (getCol() != 0)
  79. OS << ':' << getCol();
  80. if (DebugLoc InlinedAtDL = getInlinedAt()) {
  81. OS << " @[ ";
  82. InlinedAtDL.print(OS);
  83. OS << " ]";
  84. }
  85. }