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(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
00020 DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
00021 
00022 DILocation *DebugLoc::get() const {
00023   return cast_or_null<DILocation>(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 DILocation *DebugLoc::getInlinedAt() const {
00042   assert(get() && "Expected valid DebugLoc");
00043   return get()->getInlinedAt();
00044 }
00045 
00046 MDNode *DebugLoc::getInlinedAtScope() const {
00047   return cast<DILocation>(Loc)->getInlinedAtScope();
00048 }
00049 
00050 DebugLoc DebugLoc::getFnDebugLoc() const {
00051   // FIXME: Add a method on \a DILocation that does this work.
00052   const MDNode *Scope = getInlinedAtScope();
00053   if (auto *SP = getDISubprogram(Scope))
00054     return DebugLoc::get(SP->getScopeLine(), 0, SP);
00055 
00056   return DebugLoc();
00057 }
00058 
00059 DebugLoc DebugLoc::get(unsigned Line, unsigned Col, const MDNode *Scope,
00060                        const MDNode *InlinedAt) {
00061   // If no scope is available, this is an unknown location.
00062   if (!Scope)
00063     return DebugLoc();
00064 
00065   return DILocation::get(Scope->getContext(), Line, Col,
00066                          const_cast<MDNode *>(Scope),
00067                          const_cast<MDNode *>(InlinedAt));
00068 }
00069 
00070 void DebugLoc::dump() const {
00071 #ifndef NDEBUG
00072   if (!Loc)
00073     return;
00074 
00075   dbgs() << getLine();
00076   if (getCol() != 0)
00077     dbgs() << ',' << getCol();
00078   if (DebugLoc InlinedAtDL = DebugLoc(getInlinedAt())) {
00079     dbgs() << " @ ";
00080     InlinedAtDL.dump();
00081   } else
00082     dbgs() << "\n";
00083 #endif
00084 }
00085 
00086 void DebugLoc::print(raw_ostream &OS) const {
00087   if (!Loc)
00088     return;
00089 
00090   // Print source line info.
00091   auto *Scope = cast<DIScope>(getScope());
00092   OS << Scope->getFilename();
00093   OS << ':' << getLine();
00094   if (getCol() != 0)
00095     OS << ':' << getCol();
00096 
00097   if (DebugLoc InlinedAtDL = getInlinedAt()) {
00098     OS << " @[ ";
00099     InlinedAtDL.print(OS);
00100     OS << " ]";
00101   }
00102 }