LLVM  mainline
DebugLoc.cpp
Go to the documentation of this file.
00001 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
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 #include "llvm/IR/DebugLoc.h"
00011 #include "LLVMContextImpl.h"
00012 #include "llvm/ADT/DenseMapInfo.h"
00013 #include "llvm/IR/DebugInfo.h"
00014 using namespace llvm;
00015 
00016 //===----------------------------------------------------------------------===//
00017 // DebugLoc Implementation
00018 //===----------------------------------------------------------------------===//
00019 DebugLoc::DebugLoc(MDLocation *L) : Loc(L) {}
00020 DebugLoc::DebugLoc(MDNode *L) : Loc(L) {}
00021 
00022 MDLocation *DebugLoc::get() const {
00023   return cast_or_null<MDLocation>(Loc.get());
00024 }
00025 
00026 unsigned DebugLoc::getLine() const {
00027   assert(get() && "Expected valid DebugLoc");
00028   return get()->getLine();
00029 }
00030 
00031 unsigned DebugLoc::getCol() const {
00032   assert(get() && "Expected valid DebugLoc");
00033   return get()->getColumn();
00034 }
00035 
00036 MDNode *DebugLoc::getScope() const {
00037   assert(get() && "Expected valid DebugLoc");
00038   return get()->getScope();
00039 }
00040 
00041 MDLocation *DebugLoc::getInlinedAt() const {
00042   assert(get() && "Expected valid DebugLoc");
00043   return get()->getInlinedAt();
00044 }
00045 
00046 MDNode *DebugLoc::getInlinedAtScope() const {
00047   return cast<MDLocation>(Loc)->getInlinedAtScope();
00048 }
00049 
00050 DebugLoc DebugLoc::getFnDebugLoc() const {
00051   // FIXME: Add a method on \a MDLocation that does this work.
00052   const MDNode *Scope = getInlinedAtScope();
00053   DISubprogram SP = getDISubprogram(Scope);
00054   if (SP.isSubprogram())
00055     return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
00056 
00057   return DebugLoc();
00058 }
00059 
00060 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
00061                        MDNode *Scope, MDNode *InlinedAt) {
00062   // If no scope is available, this is an unknown location.
00063   if (!Scope)
00064     return DebugLoc();
00065 
00066   return MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt);
00067 }
00068 
00069 void DebugLoc::dump() const {
00070 #ifndef NDEBUG
00071   if (!Loc)
00072     return;
00073 
00074   dbgs() << getLine();
00075   if (getCol() != 0)
00076     dbgs() << ',' << getCol();
00077   if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
00078     dbgs() << " @ ";
00079     InlinedAtDL.dump();
00080   } else
00081     dbgs() << "\n";
00082 #endif
00083 }
00084 
00085 void DebugLoc::print(raw_ostream &OS) const {
00086   if (!Loc)
00087     return;
00088 
00089   // Print source line info.
00090   DIScope Scope(getScope());
00091   assert((!Scope || Scope.isScope()) &&
00092          "Scope of a DebugLoc should be null or a DIScope.");
00093   if (Scope)
00094     OS << Scope.getFilename();
00095   else
00096     OS << "<unknown>";
00097   OS << ':' << getLine();
00098   if (getCol() != 0)
00099     OS << ':' << getCol();
00100 
00101   if (DebugLoc InlinedAtDL = getInlinedAt()) {
00102     OS << " @[ ";
00103     InlinedAtDL.print(OS);
00104     OS << " ]";
00105   }
00106 }