Emit debug information for global and static variables when -g is specified.

llvm-svn: 51993
This commit is contained in:
Sanjiv Gupta 2008-06-05 08:59:10 +00:00
parent e0c5adc158
commit 158143ad54
4 changed files with 70 additions and 0 deletions

View File

@ -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<llvm::GlobalVariableDesc *>::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);
}

View File

@ -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<llvm::DebugInfoDesc *> RegionStack;
std::vector<llvm::VariableDesc *> VariableDescList;
std::vector<llvm::GlobalVariableDesc *> 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.

View File

@ -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<llvm::GlobalVariable *>(GV), &D);
}
}
/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a

View File

@ -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