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   // Saturate line and col to "unknown".
00057   // FIXME: Allow 16-bits for columns.
00058   if (Col > 255) Col = 0;
00059   if (Line >= (1 << 24)) Line = 0;
00060 
00061   LLVMContext &Context = Scope->getContext();
00062   Type *Int32 = Type::getInt32Ty(Context);
00063   Metadata *Elts[] = {ConstantAsMetadata::get(ConstantInt::get(Int32, Line)),
00064                       ConstantAsMetadata::get(ConstantInt::get(Int32, Col)),
00065                       Scope, InlinedAt};
00066   return getFromDILocation(MDNode::get(Context, Elts));
00067 }
00068 
00069 /// getAsMDNode - This method converts the compressed DebugLoc node into a
00070 /// DILocation-compatible MDNode.
00071 MDNode *DebugLoc::getAsMDNode() const { return Loc; }
00072 
00073 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
00074 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
00075   DebugLoc Loc;
00076   Loc.Loc.reset(N);
00077   return Loc;
00078 }
00079 
00080 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
00081 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
00082   DILexicalBlock LexBlock(N);
00083   MDNode *Scope = LexBlock.getContext();
00084   if (!Scope) return DebugLoc();
00085   return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
00086              nullptr);
00087 }
00088 
00089 void DebugLoc::dump() const {
00090 #ifndef NDEBUG
00091   if (!isUnknown()) {
00092     dbgs() << getLine();
00093     if (getCol() != 0)
00094       dbgs() << ',' << getCol();
00095     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
00096     if (!InlinedAtDL.isUnknown()) {
00097       dbgs() << " @ ";
00098       InlinedAtDL.dump();
00099     } else
00100       dbgs() << "\n";
00101   }
00102 #endif
00103 }
00104 
00105 void DebugLoc::print(raw_ostream &OS) const {
00106   if (!isUnknown()) {
00107     // Print source line info.
00108     DIScope Scope(getScope());
00109     assert((!Scope || Scope.isScope()) &&
00110            "Scope of a DebugLoc should be null or a DIScope.");
00111     if (Scope)
00112       OS << Scope.getFilename();
00113     else
00114       OS << "<unknown>";
00115     OS << ':' << getLine();
00116     if (getCol() != 0)
00117       OS << ':' << getCol();
00118     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
00119     if (!InlinedAtDL.isUnknown()) {
00120       OS << " @[ ";
00121       InlinedAtDL.print(OS);
00122       OS << " ]";
00123     }
00124   }
00125 }