DebugLoc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
  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. // This file defines a number of light weight data structures used
  10. // to describe and track debug location information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_DEBUGLOC_H
  14. #define LLVM_IR_DEBUGLOC_H
  15. #include "llvm/IR/TrackingMDRef.h"
  16. #include "llvm/Support/DataTypes.h"
  17. namespace llvm {
  18. class LLVMContext;
  19. class raw_ostream;
  20. class DILocation;
  21. /// A debug info location.
  22. ///
  23. /// This class is a wrapper around a tracking reference to an \a DILocation
  24. /// pointer.
  25. ///
  26. /// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a
  27. /// one based on relatively opaque \a MDNode pointers.
  28. class DebugLoc {
  29. TrackingMDNodeRef Loc;
  30. public:
  31. DebugLoc() = default;
  32. /// Construct from an \a DILocation.
  33. DebugLoc(const DILocation *L);
  34. /// Construct from an \a MDNode.
  35. ///
  36. /// Note: if \c N is not an \a DILocation, a verifier check will fail, and
  37. /// accessors will crash. However, construction from other nodes is
  38. /// supported in order to handle forward references when reading textual
  39. /// IR.
  40. explicit DebugLoc(const MDNode *N);
  41. /// Get the underlying \a DILocation.
  42. ///
  43. /// \pre !*this or \c isa<DILocation>(getAsMDNode()).
  44. /// @{
  45. DILocation *get() const;
  46. operator DILocation *() const { return get(); }
  47. DILocation *operator->() const { return get(); }
  48. DILocation &operator*() const { return *get(); }
  49. /// @}
  50. /// Check for null.
  51. ///
  52. /// Check for null in a way that is safe with broken debug info. Unlike
  53. /// the conversion to \c DILocation, this doesn't require that \c Loc is of
  54. /// the right type. Important for cases like \a llvm::StripDebugInfo() and
  55. /// \a Instruction::hasMetadata().
  56. explicit operator bool() const { return Loc; }
  57. /// Check whether this has a trivial destructor.
  58. bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }
  59. enum { ReplaceLastInlinedAt = true };
  60. /// Rebuild the entire inlined-at chain for this instruction so that the top of
  61. /// the chain now is inlined-at the new call site.
  62. /// \param InlinedAt The new outermost inlined-at in the chain.
  63. static DebugLoc appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt,
  64. LLVMContext &Ctx,
  65. DenseMap<const MDNode *, MDNode *> &Cache);
  66. unsigned getLine() const;
  67. unsigned getCol() const;
  68. MDNode *getScope() const;
  69. DILocation *getInlinedAt() const;
  70. /// Get the fully inlined-at scope for a DebugLoc.
  71. ///
  72. /// Gets the inlined-at scope for a DebugLoc.
  73. MDNode *getInlinedAtScope() const;
  74. /// Find the debug info location for the start of the function.
  75. ///
  76. /// Walk up the scope chain of given debug loc and find line number info
  77. /// for the function.
  78. ///
  79. /// FIXME: Remove this. Users should use DILocation/DILocalScope API to
  80. /// find the subprogram, and then DILocation::get().
  81. DebugLoc getFnDebugLoc() const;
  82. /// Return \c this as a bar \a MDNode.
  83. MDNode *getAsMDNode() const { return Loc; }
  84. /// Check if the DebugLoc corresponds to an implicit code.
  85. bool isImplicitCode() const;
  86. void setImplicitCode(bool ImplicitCode);
  87. bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
  88. bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; }
  89. void dump() const;
  90. /// prints source location /path/to/file.exe:line:col @[inlined at]
  91. void print(raw_ostream &OS) const;
  92. };
  93. } // end namespace llvm
  94. #endif // LLVM_IR_DEBUGLOC_H