From: Sanjiv Gupta Date: Thu, 5 Jun 2008 08:59:10 +0000 (+0000) Subject: Emit debug information for global and static variables when -g is specified. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=686226b538e72c5059ab7c9a8f87eb883193b645;p=clang Emit debug information for global and static variables when -g is specified. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51993 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index 19745dc2a0..009245c112 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -43,8 +43,10 @@ CGDebugInfo::CGDebugInfo(CodeGenModule *m) , RegionEndFn(NULL) , CompileUnitAnchor(NULL) , SubprogramAnchor(NULL) +, GlobalVariableAnchor(NULL) , RegionStack() , VariableDescList() +, GlobalVarDescList(NULL) , Subprogram(NULL) { SR = new llvm::DISerializer(); @@ -79,8 +81,14 @@ CGDebugInfo::~CGDebugInfo() delete *I; } + for (std::vector::iterator I + = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) { + delete *I; + } + delete CompileUnitAnchor; delete SubprogramAnchor; + delete GlobalVariableAnchor; } void CGDebugInfo::setLocation(SourceLocation loc) @@ -566,3 +574,41 @@ void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag, Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), ""); } +/// EmitGlobalVariable - Emit information about a global variable. +void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV, + const VarDecl *decl) +{ + // Create global variable debug descriptor. + llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc(); + + // Push it onto the list so that we can free it. + GlobalVarDescList.push_back(Global); + + // Make sure we have an anchor. + if (!GlobalVariableAnchor) + GlobalVariableAnchor = new llvm::AnchorDesc(Global); + + // Get name information. + Global->setName(decl->getName()); + Global->setFullName(decl->getName()); + + llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); + SourceManager &SM = M->getContext().getSourceManager(); + uint64_t Loc = SM.getLogicalLineNumber(CurLoc); + + llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit); + + // Fill in the Global information. + Global->setAnchor(GlobalVariableAnchor); + Global->setContext(Unit); + Global->setFile(Unit); + Global->setLine(Loc); + Global->setType(TyD); + Global->setIsDefinition(true); + Global->setIsStatic(GV->hasInternalLinkage()); + Global->setGlobalVariable(GV); + + // Make sure global is created if needed. + getValueFor(Global); +} + diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h index 3e7835a2d0..35d167ce81 100644 --- a/lib/CodeGen/CGDebugInfo.h +++ b/lib/CodeGen/CGDebugInfo.h @@ -32,6 +32,8 @@ namespace llvm { class TypeDesc; class VariableDesc; class SubprogramDesc; + class GlobalVariable; + class GlobalVariableDesc; } namespace clang { @@ -63,8 +65,10 @@ private: llvm::Function *RegionEndFn; llvm::AnchorDesc *CompileUnitAnchor; llvm::AnchorDesc *SubprogramAnchor; + llvm::AnchorDesc *GlobalVariableAnchor; std::vector RegionStack; std::vector VariableDescList; + std::vector GlobalVarDescList; llvm::SubprogramDesc *Subprogram; /// Helper functions for getOrCreateType. @@ -105,6 +109,9 @@ public: /// EmitDeclare - Emit call to llvm.dbg.declare for a variable declaration. void EmitDeclare(const VarDecl *decl, unsigned Tag, llvm::Value *AI, llvm::IRBuilder &Builder); + + /// EmitGlobalVariable - Emit information about a global variable. + void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *decl); /// getOrCreateCompileUnit - Get the compile unit from the cache or create a /// new one if necessary. diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 8723a66872..5451d4afc0 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -116,6 +116,15 @@ void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { } DMEntry = GV; + + // Emit global variable debug descriptor for static vars. + CGDebugInfo *DI = CGM.getDebugInfo(); + if(DI) { + if(D.getLocation().isValid()) + DI->setLocation(D.getLocation()); + DI->EmitGlobalVariable(static_cast(GV), &D); + } + } /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 5be6872a1e..b802ce7d97 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -739,6 +739,14 @@ void CodeGenModule::EmitGlobalVarInit(const VarDecl *D) { break; } } + + // Emit global variable debug information. + CGDebugInfo *DI = getDebugInfo(); + if(DI) { + if(D->getLocation().isValid()) + DI->setLocation(D->getLocation()); + DI->EmitGlobalVariable(GV, D); + } } /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified