LLVM API Documentation

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 
00020 unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); }
00021 unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); }
00022 
00023 MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); }
00024 
00025 MDNode *DebugLoc::getInlinedAt() const {
00026   return DILocation(Loc).getOrigLocation();
00027 }
00028 
00029 /// Return both the Scope and the InlinedAt values.
00030 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
00031   Scope = getScope();
00032   IA = getInlinedAt();
00033 }
00034 
00035 MDNode *DebugLoc::getScopeNode() const {
00036   if (MDNode *InlinedAt = getInlinedAt())
00037     return DebugLoc::getFromDILocation(InlinedAt).getScopeNode();
00038   return getScope();
00039 }
00040 
00041 DebugLoc DebugLoc::getFnDebugLoc() const {
00042   const MDNode *Scope = getScopeNode();
00043   DISubprogram SP = getDISubprogram(Scope);
00044   if (SP.isSubprogram())
00045     return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
00046 
00047   return DebugLoc();
00048 }
00049 
00050 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
00051                        MDNode *Scope, MDNode *InlinedAt) {
00052   // If no scope is available, this is an unknown location.
00053   if (!Scope)
00054     return DebugLoc();
00055 
00056   return getFromDILocation(
00057       MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt));
00058 }
00059 
00060 /// getAsMDNode - This method converts the compressed DebugLoc node into a
00061 /// DILocation-compatible MDNode.
00062 MDNode *DebugLoc::getAsMDNode() const { return Loc; }
00063 
00064 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
00065 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
00066   DebugLoc Loc;
00067   Loc.Loc.reset(N);
00068   return Loc;
00069 }
00070 
00071 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
00072 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
00073   DILexicalBlock LexBlock(N);
00074   MDNode *Scope = LexBlock.getContext();
00075   if (!Scope) return DebugLoc();
00076   return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
00077              nullptr);
00078 }
00079 
00080 void DebugLoc::dump() const {
00081 #ifndef NDEBUG
00082   if (!isUnknown()) {
00083     dbgs() << getLine();
00084     if (getCol() != 0)
00085       dbgs() << ',' << getCol();
00086     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
00087     if (!InlinedAtDL.isUnknown()) {
00088       dbgs() << " @ ";
00089       InlinedAtDL.dump();
00090     } else
00091       dbgs() << "\n";
00092   }
00093 #endif
00094 }
00095 
00096 void DebugLoc::print(raw_ostream &OS) const {
00097   if (!isUnknown()) {
00098     // Print source line info.
00099     DIScope Scope(getScope());
00100     assert((!Scope || Scope.isScope()) &&
00101            "Scope of a DebugLoc should be null or a DIScope.");
00102     if (Scope)
00103       OS << Scope.getFilename();
00104     else
00105       OS << "<unknown>";
00106     OS << ':' << getLine();
00107     if (getCol() != 0)
00108       OS << ':' << getCol();
00109     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
00110     if (!InlinedAtDL.isUnknown()) {
00111       OS << " @[ ";
00112       InlinedAtDL.print(OS);
00113       OS << " ]";
00114     }
00115   }
00116 }