DebugLoc.h 4.4 KB

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