LLVM  mainline
DebugLoc.h
Go to the documentation of this file.
00001 //===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a number of light weight data structures used
00011 // to describe and track debug location information.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_DEBUGLOC_H
00016 #define LLVM_IR_DEBUGLOC_H
00017 
00018 #include "llvm/IR/TrackingMDRef.h"
00019 #include "llvm/Support/DataTypes.h"
00020 
00021 namespace llvm {
00022 
00023   class LLVMContext;
00024   class raw_ostream;
00025   class DILocation;
00026 
00027   /// \brief A debug info location.
00028   ///
00029   /// This class is a wrapper around a tracking reference to an \a DILocation
00030   /// pointer.
00031   ///
00032   /// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a
00033   /// one based on relatively opaque \a MDNode pointers.
00034   class DebugLoc {
00035     TrackingMDNodeRef Loc;
00036 
00037   public:
00038     DebugLoc() {}
00039     DebugLoc(DebugLoc &&X) : Loc(std::move(X.Loc)) {}
00040     DebugLoc(const DebugLoc &X) : Loc(X.Loc) {}
00041     DebugLoc &operator=(DebugLoc &&X) {
00042       Loc = std::move(X.Loc);
00043       return *this;
00044     }
00045     DebugLoc &operator=(const DebugLoc &X) {
00046       Loc = X.Loc;
00047       return *this;
00048     }
00049 
00050     /// \brief Construct from an \a DILocation.
00051     DebugLoc(const DILocation *L);
00052 
00053     /// \brief Construct from an \a MDNode.
00054     ///
00055     /// Note: if \c N is not an \a DILocation, a verifier check will fail, and
00056     /// accessors will crash.  However, construction from other nodes is
00057     /// supported in order to handle forward references when reading textual
00058     /// IR.
00059     explicit DebugLoc(const MDNode *N);
00060 
00061     /// \brief Get the underlying \a DILocation.
00062     ///
00063     /// \pre !*this or \c isa<DILocation>(getAsMDNode()).
00064     /// @{
00065     DILocation *get() const;
00066     operator DILocation *() const { return get(); }
00067     DILocation *operator->() const { return get(); }
00068     DILocation &operator*() const { return *get(); }
00069     /// @}
00070 
00071     /// \brief Check for null.
00072     ///
00073     /// Check for null in a way that is safe with broken debug info.  Unlike
00074     /// the conversion to \c DILocation, this doesn't require that \c Loc is of
00075     /// the right type.  Important for cases like \a llvm::StripDebugInfo() and
00076     /// \a Instruction::hasMetadata().
00077     explicit operator bool() const { return Loc; }
00078 
00079     /// \brief Check whether this has a trivial destructor.
00080     bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }
00081 
00082     /// \brief Create a new DebugLoc.
00083     ///
00084     /// Create a new DebugLoc at the specified line/col and scope/inline.  This
00085     /// forwards to \a DILocation::get().
00086     ///
00087     /// If \c !Scope, returns a default-constructed \a DebugLoc.
00088     ///
00089     /// FIXME: Remove this.  Users should use DILocation::get().
00090     static DebugLoc get(unsigned Line, unsigned Col, const MDNode *Scope,
00091                         const MDNode *InlinedAt = nullptr);
00092 
00093     unsigned getLine() const;
00094     unsigned getCol() const;
00095     MDNode *getScope() const;
00096     DILocation *getInlinedAt() const;
00097 
00098     /// \brief Get the fully inlined-at scope for a DebugLoc.
00099     ///
00100     /// Gets the inlined-at scope for a DebugLoc.
00101     MDNode *getInlinedAtScope() const;
00102 
00103     /// \brief Find the debug info location for the start of the function.
00104     ///
00105     /// Walk up the scope chain of given debug loc and find line number info
00106     /// for the function.
00107     ///
00108     /// FIXME: Remove this.  Users should use DILocation/DILocalScope API to
00109     /// find the subprogram, and then DILocation::get().
00110     DebugLoc getFnDebugLoc() const;
00111 
00112     /// \brief Return \c this as a bar \a MDNode.
00113     MDNode *getAsMDNode() const { return Loc; }
00114 
00115     bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
00116     bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; }
00117 
00118     void dump() const;
00119 
00120     /// \brief prints source location /path/to/file.exe:line:col @[inlined at]
00121     void print(raw_ostream &OS) const;
00122   };
00123 
00124 } // end namespace llvm
00125 
00126 #endif /* LLVM_SUPPORT_DEBUGLOC_H */