From ff7d0e9509cf8f359fe14c05dc29a05a293ce88f Mon Sep 17 00:00:00 2001 From: Torok Edwin Date: Tue, 10 Mar 2009 13:41:26 +0000 Subject: Global variables don't have a corresponding llvm.dbg.declare, yet it is possible to obtain debug info about them. Introduce helpers to access debug info for global variables. Also introduce a helper that works for both local and global variables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66541 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/DebugInfo.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'lib/Analysis/DebugInfo.cpp') diff --git a/lib/Analysis/DebugInfo.cpp b/lib/Analysis/DebugInfo.cpp index 4e229e9669..d9f0aa53e9 100644 --- a/lib/Analysis/DebugInfo.cpp +++ b/lib/Analysis/DebugInfo.cpp @@ -841,6 +841,34 @@ namespace llvm { return 0; } + Value *findDbgGlobalDeclare(GlobalVariable *V) + { + const Module *M = V->getParent(); + const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type"); + if (!Ty) + return 0; + Ty = PointerType::get(Ty, 0); + + Value *Val = V->stripPointerCasts(); + for (Value::use_iterator I = Val->use_begin(), E =Val->use_end(); + I != E; ++I) { + if (ConstantExpr *CE = dyn_cast(I)) { + if (CE->getOpcode() == Instruction::BitCast) { + Value *VV = CE; + while (VV->hasOneUse()) { + VV = *VV->use_begin(); + } + if (VV->getType() == Ty) + return VV; + } + } + } + + if (Val->getType() == Ty) + return Val; + return 0; + } + /// Finds the dbg.declare intrinsic corresponding to this value if any. /// It looks through pointer casts too. const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) @@ -864,6 +892,36 @@ namespace llvm { } return 0; } + + bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type, + unsigned &LineNo, std::string &File, std::string &Dir) + { + DICompileUnit Unit; + DIType TypeD; + if (GlobalVariable *GV = dyn_cast(const_cast(V))) { + Value *DIGV = findDbgGlobalDeclare(GV); + if (!DIGV) + return false; + DIGlobalVariable Var(cast(DIGV)); + Var.getDisplayName(DisplayName); + LineNo = Var.getLineNumber(); + Unit = Var.getCompileUnit(); + TypeD = Var.getType(); + } else { + const DbgDeclareInst *DDI = findDbgDeclare(V); + if (!DDI) + return false; + DIVariable Var(cast(DDI->getVariable())); + Var.getName(DisplayName); + LineNo = Var.getLineNumber(); + Unit = Var.getCompileUnit(); + TypeD = Var.getType(); + } + TypeD.getName(Type); + Unit.getFilename(File); + Unit.getDirectory(Dir); + return true; + } } /// dump - print compile unit. -- cgit v1.2.3-18-g5258